JavaScript-AngularJS承诺不返回任何内容
我试图用JavaScript-AngularJS承诺不返回任何内容,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,我试图用Jasmine在单元测试中返回一个简单的HTTP帖子,但它似乎不起作用。该应用程序在web上运行良好。我已经测试了几个独立的函数。但这行不通 describe('Service: Auth',function(){ beforeEach(function () { module('ui.router'); module('main'); module('users'); }); var AuthFactory,
Jasmine
在单元测试中返回一个简单的HTTP帖子,但它似乎不起作用。该应用程序在web上运行良好。我已经测试了几个独立的函数。但这行不通
describe('Service: Auth',function(){
beforeEach(function () {
module('ui.router');
module('main');
module('users');
});
var AuthFactory, httpBackend;
beforeEach(inject(function($httpBackend, _AuthFactory_) {
httpBackend = $httpBackend;
AuthFactory = _AuthFactory_;
}));
it('should return POST', function() {
AuthFactory.signIn({inputUser: {username: "admin"}, passInput: {password: "adminpass"}}).then(
function(result) {
console.log('======== SUCCESS ========');
console.log(result);
},
function(err) {
console.log('======== ERROR ========');
console.log(err);
},
function(progress) {
console.log('======== PROGRESS ========');
console.log(progress);
}
);
console.log('HTTP call finished.');
expect(1).toBe(1);
});
});
这是工厂:
angular.module('users').factory('AuthFactory', ['$http', function($http) {
var AuthFactory = {};
AuthFactory.signIn = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthFactoryServ/signIn', data);
};
AuthFactory.signOut = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthFactoryServ/signOut', data);
};
return AuthFactory;
}]);
这就是我得到的:
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 1 SUCCESS (0 s
LOG: Object{$$state: Object{status: 0}, success: function (fn)
{ ... }, error: function (fn) { ... }}
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 1 SUCCESS (0 s
LOG: 'HTTP call finished.'
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 1 SUCCESS (0 s
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 1 of 1 SUCCESS (0 s
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 1 of 1 SUCCESS (0 secs / 0.022 secs)
我已经通过Postman测试了HTTP调用,它成功地返回了数据。所以我做错了什么
谢谢。AuthFactory.signIn是异步的,并返回一个承诺-在承诺得到解决之前,您的测试函数已经完成 你需要告诉Jasmine它应该:
@A我只是想测试一下我的服务功能是否正常。。。嗯,我在factory方法中创建了一个新函数,比如AuthFactory.test来返回一个字符串,当我把它放在Jasmine测试中时,它成功地返回了这个字符串。哦,我明白了。好的,当我运行它时,我得到:
TypeError:“undefined”不是一个函数(计算”结果。然后(function(){
在}行上)。总是(完成)
但我不明白….@djp我已将始终更改为最终,因为这是在尝试删除var result
和chainresult中可用的。然后直接从AuthFactory.sign
it('should return POST', function(done) {
var result = AuthFactory.signIn({inputUser: {username: "admin"}, passInput: {password: "adminpass"}}).then(
function(result) {
console.log('======== SUCCESS ========');
console.log(result);
},
function(err) {
console.log('======== ERROR ========');
console.log(err);
},
function(progress) {
console.log('======== PROGRESS ========');
console.log(progress);
}
);
result.then(function(){
console.log('HTTP call finished.');
expect(1).toBe(1);
}).finally(done); //here we hook Jasmine callback to promise chain
});