我正在开发有角度的网页应用。 下面是我的控制器中的一个方法,它会在发生错误时将fetchPlayersFailed设置为true:
$scope.fetchAllPlayers = function() {
$scope.players = PlayerFact.query({
active: true
},
undefined,
function() {
console.log('set fetchPlayersFailed to true');
$scope.fetchPlayersFailed = true;
}
);
};
但是当我运行单元测试时,此方法发生错误,下面是我编写的规范:
it('fetchAllPlayers: should set fetchPlayersFailed to true when error happens', function() {
httpBackend.expectGET('/api/players?active=true').respond(500);
scope.fetchAllPlayers();
httpBackend.flush();
expect(scope.fetchPlayersFailed).toEqual(true);
});
这是错误:
Chrome 32.0 (Windows) Controller: PlayerCtrl fetchAllPlayers: should set fetchPl
ayersFailed to true when error happens FAILED
Expected false to equal true.
Error: Expected false to equal true.
at null.<anonymous> (C:/Users/I056958/Documents/My Own/javascript wo
rkspace/redjoker/test/spec/controllers/player.js:111:38)
为什么fetchPlayersFailed没有设置为true ??
答案 0 :(得分:1)
我建议在你的测试中使用GET('url')。响应(500),因为你没有测试你的测试中的电话。 此外,如果PlayerFact是一个返回$ resource对象的服务,我认为最好先模拟它。
您的问题在于查询中的函数,它是成功回调而不是错误处理程序,即如果PlayerFact是$ resource对象。 这是您更新的代码:
$scope.fetchAllPlayers = function() { $scope.players = PlayerFact.query({ active: true }, undefined, function(){ }, function() { console.log('set fetchPlayersFailed to true'); $scope.fetchPlayersFailed = true; }); };