代码运行时如何使Jasminetesting失败

我正在testing一个AngularJS服务,并且作为其中的一部分,我想确保从不调用某个callback函数。 现在,我的testing看起来像

it('should succeed to login the user', function () { var params = { email: 'foo@bar.com', password: 'hunter2' }; var member = { remoteAddress: '1.2.3.4' }; $httpBackend.expectPOST(fakeApiUrl + '/1/authentication/login', params) .respond(200, member); auth.login(params.email, params.password).then( function (m) { expect(m.remoteAddress).toBe(member.remoteAddress); }, function () { // I want this to be fail() expect(true).toBe(false); } ); $rootScope.$digest(); $httpBackend.flush(); }); 

问题是,为了使testing失败,如果该callback被触发,我不得不做一些像expect(true).toBe(false)脏。

有没有更好的方法来完成这个? 比如,我可以使用一个更合适的匹配器,还是应该以不同的方式构build我的testing?

一个办法是build立一个间谍,并声称间谍从来没有被称为…

 var mySpy = jasmine.createSpy('mySpy'); $httpBackend.expectPOST(fakeApiUrl + '/1/authentication/login', params) .respond(200, member); auth.login(params.email, params.password).then( function (m) { expect(m.remoteAddress).toBe(member.remoteAddress); }, mySpy ); $rootScope.$digest(); $httpBackend.flush(); expect(mySpy).not.toHaveBeenCalled(); 
 it('should succeed to login the user', function () { var params = { email: 'foo@bar.com', password: 'hunter2' }; auth.login = jasmine.createSpy().andReturn($q.reject()); auth.login(params.email, params.password).then( function (m) { expect(m.remoteAddress).toBe(member.remoteAddress); }, function () { // I want this to be fail() expect(true).toBe(false); }); $scope.$digest(); }); 

http://jsfiddle.net/zk8Lg/1/