简体   繁体   English

在Angular单元测试中模拟ngResource

[英]Mocking ngResource in Angular unit tests

I have an ngResourceMockFactory which looks like this: 我有一个ngResourceMockFactory ,如下所示:

(function() {
  'use strict';

  angular.module('app')
    .factory('NgResourceMock', ngResourceMockFactory)
  ;

  ngResourceMockFactory.$inject = [];

  function ngResourceMockFactory() {
    function NgResourceMock() {
      var context = this;

      context.$promise.then = function() {
        context.prototype.$promise.then.apply(context, arguments);
      };

      context.$promise.finally = function() {
        context.prototype.$promise.finally.apply(context, arguments);
      };
    }

    NgResourceMock.prototype.$promise = {
      then: function(onSuccess, onError) {
        this.$promise.onSuccess = onSuccess;
        this.$promise.onError = onError;
      },
      finally: function(onComplete) {
        this.$promise.onComplete = onComplete;
      }
    };

    return NgResourceMock;
  }
})();

I inject this into my tests in a beforeEach like so: 我注入我在测试这个beforeEach像这样:

beforeEach(inject(function(NgResourceMock) {
  ngResourceMock = new NgResourceMock();
}));

then I use it like this: 然后我像这样使用它:

describe('initiateWorkflow function', function() {
  beforeEach(function() {
    vm.player = {id: 123};
    spyOn(dataService, 'initiateWorkflow').and.returnValue(ngResourceMock);
    vm.initiateWorkflow();
  });

  it('should call dataService.initiateWorkflow', function() {
    expect(dataService.initiateWorkflow).toHaveBeenCalledWith({playerId: vm.player.id}, {});
  });
});

but I keep seeing the following error: 但我一直看到以下错误:

TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise')

This leads me to believe that something is wrong with my ngResourceMockFactory , but I'm not sure what it is. 这让我相信我的ngResourceMockFactory ,但我不确定它是什么。

Don't know if this can be of any help, but if you are trying to evaluate asynchronous operations in your tests, you may want to use the done() method in Jasmine. 不知道这是否有任何帮助,但是如果你试图评估测试中的异步操作,你可能想在Jasmine中使用done()方法。

As per their documentation: 根据他们的文件:

beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
});

by passing done as a parameter of the beforeEach callback, any test run after the before each will wait until the done() function has been called. 通过传递done作为beforeEach回调的参数,在每个之前的任何测试运行将等到调用done()函数。

Source: Jasmine (Asynchronous Support section). 资料来源: Jasmine (异步支持部分)。

Hope this helps. 希望这可以帮助。

Here is the solution to your problem. 这是您的问题的解决方案。

The error TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise') is caused when you try to invoke the promise object before invoking the function into which it is defined or into which your parent function is defined. 错误TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise')是在尝试调用promise对象之前调用定义它的函数或定义了父函数时引起的。

Here the returnValue(ngResourceMock) is directly calling into the function without the context and parameters need to be defined. 这里的returnValue(ngResourceMock)直接调用函数而不需要定义上下文和参数。

Therefore you can try to add another beforeEach statement like 因此,您可以尝试添加另一个beforeEach语句

beforeEach(angular.mock.module(app));

to load your app module 加载您的应用模块

Here may be the same concept related to your problem another link here. 这里可能是与您的问题相关的另一个链接。

Hope it may help you a bit. 希望它可能对你有所帮助。

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

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