數據簡單地說:
我可以匹配模擬轉$ httpBackend從$資源服務數據在茉莉單元測試?我會怎麼做?
我想:我都魅影威脅的信息存儲
it("should return movie search data from the title", function() {
var movieData = { title: "The Phantom Menace", description: "A movie.";
var response = [];
$httpBackend.when('GET', 'http://www.omdbapi.com/?v=1&s=the%20phantom%20menace')
.respond(200, movieData);
moviesService.search('the phantom menace')
.then(function onSuccess(data) {
response = data;
});
$httpBackend.flush();
expect(response.data).toEqual(movieData);
}); // end it
:
當我茉莉花測試我的後端服務,$ httpBackend和$ http服務,我能做到這一點在movieData。此測試通過是因爲它將從模擬後端接收的數據與應該是的數據(movieData)進行比較。
什麼我得到:
當我茉莉花測試我的後端服務,$ httpBackend和$資源服務,情況就不同了。
it("retrieves a bunch objects", function() {
var result = {}, testArray = [];
var dataToCheck = [
{ id: 1, "des": "This is one."},
{ id: 2, "des": "This is two."},
{ id: 3, "des": "This is three."}
];
$httpBackend.whenGET('movies/api')
.respond(dataToCheck);
MovieResource.query().$promise.then(function (data) {
result = data;
});
$httpBackend.flush();
angular.forEach(result, function (resource) {
testArray.push(resource);
})
expect(testArray).toEqual(dataToCheck);
}); // end it
噶告訴我:
預期[資源({ID:1,DES: '這是一個'}),資源({ID:2, DES:「這是')),Resource({id:3,des:'This is three。'))] to equal [Object({id:1,des:'This is one。')),Object({id :2, des:'This is two。')),Object({id:3,des:'This is three。'})]。
所以,而不是「對象」,他們是「資源」。
我很感謝你可以給我的任何指導。
是否有可能投在dataToCheck作爲資源的JSON對象? – PunDefeated