简体   繁体   English

如何用$ resource测试在AngularJS中返回承诺的工厂?

[英]How to test a factory with $resource which returns a promise in AngularJS?

In my Angular application, I have a controller defined as follows: 在我的Angular应用程序中,我有一个定义如下的controller

angular.module('myApp.controllers')
  .controller('AppController', function($rootScope, $scope, CheckResource) {
    $scope.check = function(data) {
      var promise = CheckResource.query(data).$promise;
      promise.then(function(result) {
         $scope.value = result;
      }, function() {
         $scope.value = "default";
      });
    };
  };

And my CheckResource is a Factory as follows: 我的CheckResource是一个Factory ,如下所示:

angular.module('myApp.services', ['ngResource'])
  .factory('CheckResource', function($resource) {
    return $resource('/check', {}, {
      query: {
        method: 'POST'
      }
    });
  });

I would like to write a unit test for my controller AppController and somehow mock my factory CheckResource , but I'm not sure how exactly I can achieve this using $q and deferred promise. 我想为我的控制器AppController编写一个单元测试,并以某种方式模拟我的工厂CheckResource ,但是我不确定如何使用$q和递延的CheckResource来实现这一目标。 Can anyone please explain me what's going on this scenario? 谁能解释一下这种情况下发生了什么?

What are my options for testing this scenario? 我有什么选择可以测试这种情况? Should I mock my service? 我应该嘲笑我的服务吗? Should I use spyOn ? 我应该使用spyOn吗? Should I use $httpBackend ? 我应该使用$httpBackend吗? Or, should I do an E2E test? 还是我应该进行E2E测试?

Assuming you have a controller mocked in your test, you can do something like: 假设您在测试中模拟了一个控制器,则可以执行以下操作:

var deferred;

beforeEach(inject(function($q) {
    deferred = $q.defer();

    myController.CheckResource = {
        query: function() {
            return {$promise: deferred.promise};
        }
    };
}));

Then in your test itself: 然后在您的测试中:

it('tests CheckResource', function() {
     deferred.resolve([]);
     expect(something).toHaveHappened();
});

ngResource is defined in the separate module. ngResource在单独的模块中定义。 Please check the documentation http://docs.angularjs.org/api/ngResource In other words you have to include and add ngResource dependency to MyApp.Factory module - angular.module("MyApp.Factory", ["ngResource"]) 请检查文档http://docs.angularjs.org/api/ngResource 。换句话说,您必须包括ngResource依赖项并将其添加到MyApp.Factory模块中-angular.module(“ MyApp.Factory”,[“ ngResource”])

var factory;

beforeEach(function () {

  module("MyApp.Factory");

  inject(function (_factory_) {
    factory = _factory_;
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 茉莉花2.0测试angularjs工厂方法返回一个承诺 - jasmine 2.0 testing angularjs factory method which returns a promise 如何测试一个返回$ timeout promise并带有$ http.get的AngularJS工厂方法? - How to test an AngularJS factory method that returns a $timeout promise with a $http.get inside? 如何对使用AngularJS中的另一个工厂的异步工厂进行单元测试? - How to unit test an asynchronus factory which uses another factory in AngularJS? AngularJS资源工厂始终返回空响应 - AngularJS resource factory always returns empty response 如何对一个调用另一个返回promise的函数进行单元测试? - How to unit test a function which calls another that returns a promise? AngularJS:在模拟使用$ http的服务时测试返回承诺的工厂 - AngularJS : testing a factory that returns a promise, while mocking a service that uses $http 如何对 angularjs 承诺进行单元测试 - How to unit test an angularjs promise 在单元测试中配置一个从延迟返回承诺的存根工厂? - Configure a stubbed factory that returns a promise from a deferred in a unit test? 如何模拟angularjs $ resource工厂 - How do you mock an angularjs $resource factory 如何从angularjs的工厂“重建” $ resource? - How to “rebuild” a $resource from a factory in angularjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM