简体   繁体   English

如何在角度单元测试中模拟服务的依赖关系?

[英]how do I mock a dependency of a service in my angular unit test?

I'm using karma, angular and mocha. 我正在使用业力,角度和摩卡。

The MyServices module I want to inject. 我要注入的MyServices模块。 It's addThing method also calls a $localStorage provider and I want to test that the $localStorage.setItem method has been implicitly called when I call MyServices.addThing(data) 它的addThing方法也调用$localStorage提供程序,我想测试在调用MyServices.addThing(data)时隐式调用$localStorage.setItem方法

Here's my code: 这是我的代码:

describe('MyServices', function() {
  var $q;
  var $rootScope;
  var $scope;
  var MyServices;
  var $timeout;
  var $httpBackend;

  // inject modules
  beforeEach(function(){
    module('my.stuff');

    var _LocalStorage_ = {
      setItem: function(){
        return 'foo';
      }
    };

    // mock the dependencies of the module we are testing
    module(function ($provide) {
      $provide.value('$localStorage', _LocalStorage_); //here is where the error occurs
    });
  });

  beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_, _MyServices_, _$httpBackend_) {
    $q = _$q_;
    $rootScope = _$rootScope_;
    $timeout = _$timeout_;
    MyServices = _MyServices_;
    $httpBackend = _$httpBackend_;
    $scope = $rootScope.$new();
  }));

  it.only('should call a method and access localStorage', function(){
    var spyAdd = sinon.spy(MyServices, 'addThing');
    var spySetItem = sinon.spy($localStorage, 'setItem');

    MyServices.addThing({ name: 'Thing' });

    expect(spyAdd.callCount).to.equal(1);
    expect(spySetItem.callCount).to.equal(1);
  })
});

When I run it I get an error that $localStorage is undefined. 当我运行它时,我收到$localStorage未定义的错误。

you don't have register that fake $localStorage, just use the original one and create a spy on the setItem method. 你没有注册假$ localStorage,只需使用原来的并在setItem方法上创建一个间谍。

so remove this 所以删除它

var _LocalStorage_ = {
  setItem: function(){
    return 'foo';
  }
};
module(function ($provide) {
  $provide.value('$localStorage', _LocalStorage_); //here is where the error occurs
});

but inject it here 但是把它注入这里

var $localStorage;
beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_, _MyServices_, _$httpBackend_, _$localStorage_) {
  $q = _$q_;
  $rootScope = _$rootScope_;
  $timeout = _$timeout_;
  MyServices = _MyServices_;
  $httpBackend = _$httpBackend_;
  $scope = $rootScope.$new();
  $localStorage = _$localStorage_; //here
}));

since you have already created a spy on the method, so nothing needs to be changed in the test. 因为你已经在方法上创建了一个间谍,所以在测试中不需要改变任何东西。

Try to declare LocalStorage outside of your beforeEach. 尝试在beforeEach之外声明LocalStorage Btw, you don't need the _, you can just call it LocalStorage. 顺便说一句,你不需要_,你可以称之为LocalStorage。

I also recommend downloading SinonJS because it has a bunch of useful methods when spying or mocking dependencies. 我还建议下载SinonJS,因为它在间谍或模拟依赖项时有很多有用的方法。

暂无
暂无

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

相关问题 如何在AngularJS Jasmine单元测试中模拟返回promise的服务? - How do I mock a service that returns promise in AngularJS Jasmine unit test? 如何对一个依赖于其注册回调的Angular服务进行单元测试? - How can I unit test an Angular service that registers a callback on its dependency? 如何将模拟服务注入过滤器的单元测试? - How can i inject a mock service into a unit test for a filter? 如何在AngularJS单元测试中模拟promise的结果? - How do I mock the result of a promise in an AngularJS unit test? 如何模拟$ window进行单元测试AngularJS服务? - how to mock $window to unit test AngularJS service? 如何为需要测试其发射的角度指令进行茉莉花单元测试? - How do I do a jasmine unit test for my angular directive that needs to test its emit? Angular Karma单元测试:如何在karma配置中注入模拟服务,而不是在所有测试规范中 - Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs 如何在Karma上使用Jasmine将一个模拟依赖项注入到angular指令中 - How do I inject a mock dependency into an angular directive with Jasmine on Karma 如何在不使用http后端的情况下对Http Get进行单元测试,我想模拟后端,而是使用jasmine和Angular 1? - How to do a unit test for Http Get without using http backend, I want to mock the backend instead, I'm using jasmine and Angular 1? 如何在Jasmine单元测试中模拟模块API依赖关系? - How to mock a module API dependency in Jasmine unit test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM