繁体   English   中英

JavaScript-AngularJS承诺不返回任何内容

[英]JavaScript - AngularJS promise not returning anything

我正在尝试在Jasmine的单元测试中返回一个简单的HTTP帖子,但它似乎不起作用。 该应用程序可以在网络上正常运行。 我已经测试了几个隔离的功能。 但这是行不通的。

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是异步的,并返回一个AuthFactory.signIn您的测试函数在AuthFactory.signIn被解决之前AuthFactory.signIn完成。

您需要告诉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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM