Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript-AngularJS承诺不返回任何内容_Javascript_Angularjs_Jasmine - Fatal编程技术网

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
和chain
result中可用的。然后直接从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
});