我试图用Jasmine编写一个测试用于我的Angular搜索功能,但是我在完成它时遇到了麻烦。
我的测试看起来像这样,
angular.module('movieSearch', [])
.factory('MovieSearch',function($http, $q){
var service = {};
var baseUrl = 'http://api.themoviedb.org/3/';
function movieseatSearch (url){
var deferred = $q.defer();
$http.jsonp(url)
.success(function(data, status, headers, config){
deferred.resolve(data.results);
})
.error(function (data, status, headers, config) {
deferred.reject();
});
return deferred.promise;
}
service.search = function(searchquery){
return movieseatSearch(baseUrl + 'search/movie?api_key=a8f7039633f2065942cd8a28d7cadad4' + '&query=' + encodeURIComponent(searchquery) + '&callback=JSON_CALLBACK')
}
return service;
});
搜索服务看起来像这样,
Expected
[ Object({ poster_path: '/mSvpKOWbyFtLro9BjfEGqUw5dXE.jpg', adult: false, overview: 'A cryptic message from Bond’s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE.', release_date: '2015-10-26', genre_ids: [ 28, 12, 80 ], id: 206647, original_title: 'Spectre', original_language: 'en', title: 'Spectre', backdrop_path: '/qSc4L05AnHbMpSk0bsHuX25vX4V.jpg', popularity: 35.166039, vote_count: 850, video: false, vote_average: 6.32 }) ]
to equal
Object({ page: 1, results: [ Object({ poster_path: '/mSvpKOWbyFtLro9BjfEGqUw5dXE.jpg', adult: false, overview: 'A cryptic message from Bond’s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE.', release_date: '2015-10-26', genre_ids: [ 28, 12, 80 ], id: 206647, original_title: 'Spectre', original_language: 'en', title: 'Spectre', backdrop_path: '/qSc4L05AnHbMpSk0bsHuX25vX4V.jpg', popularity: 35.166039, vote_count: 850, video: false, vote_average: 6.32 }) ], total_results: 1, total_pages: 1 }).
但是当我运行测试时出现错误,
http://api.themoviedb.org/3/search/movie?api_key=a8f7039633f2065942cd8a28d7cadad4&query=james%20spectre&callback=JSON_CALLBACK
我已复制链接movieData
中的数据并将其插入var movieData = {
"page":1,"results":[{"poster_path":"\/mSvpKOWbyFtLro9BjfEGqUw5dXE.jpg","adult":false,"overview":"A cryptic message from Bond’s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE.","release_date":"2015-10-26","genre_ids":[28,12,80],"id":206647,"original_title":"Spectre","original_language":"en","title":"Spectre","backdrop_path":"\/qSc4L05AnHbMpSk0bsHuX25vX4V.jpg","popularity":35.166039,"vote_count":850,"video":false,"vote_average":6.32}],"total_results":1,"total_pages":1
}
变量。
func()
那么为什么预期数据与我提供的数据不同?
答案 0 :(得分:0)
不是100%确定它是否是anwser,但我确实找到了一些东西。在我的搜索功能中,我使用deferred.resolve(data.results);
。没有.results
测试成功,但我的搜索功能没有正确输出数据。但是将.results
添加到toEqual中的movieData变量可以修复它。 expect(response).toEqual(movieData.results);
该行也应该在刷新操作之后。