[英]JavaScript - AngularJS promise not returning anything
I am trying to return a simple HTTP post on a unit test with Jasmine
but it seems to not work. 我正在尝试在
Jasmine
的单元测试中返回一个简单的HTTP帖子,但它似乎不起作用。 The application works fine on the web. 该应用程序可以在网络上正常运行。 I have tested several isolated functions.
我已经测试了几个隔离的功能。 But this won't work.
但这是行不通的。
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);
});
});
and here is the factory : 这是工厂 :
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;
}]);
This is what I get: 这是我得到的:
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)
I have tested the HTTP calls via Postman and it successfully returns data back. 我已经通过Postman测试了HTTP调用,它成功返回了数据。 So... what am I doing wrong?
所以...我在做什么错?
Thank you. 谢谢。
The AuthFactory.signIn
is asynchronous and returns a promise - your test function is finished before the promise is resolved. AuthFactory.signIn
是异步的,并返回一个AuthFactory.signIn
您的测试函数在AuthFactory.signIn
被解决之前AuthFactory.signIn
完成。
You need to tell Jasmine that it should wait for the asynchronous test to complete : 您需要告诉Jasmine它应该等待异步测试完成 :
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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.