簡體   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