什么时候应该使用$提供与在我的角度JS单元测试茉莉花间谍 [英] When should I use $provide versus Jasmine Spies in my Angular JS Unit tests

查看:193
本文介绍了什么时候应该使用$提供与在我的角度JS单元测试茉莉花间谍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个大角度应用工作,首先我们通过使用$提供模拟服务做了很多我们的测试。但是,我们现在有很多在我们的测试茉莉花间谍,以存根和窥探的服务。

I work on a large Angular App and initially we done a lot of our tests by using $provide to mock services. However we now have a lot of Jasmine Spies in our tests in order to stub and spy on services.

spyOn(myService, 'myMethod').andReturn 'myValue'

我们应该真正使用$提供此还是有哪里上的服务的间谍是最好的方法?案例

Should we really be using $provide for this or are there cases where spying on a service is the best approach?

在他们使用的间谍为<一期角测试href=\"https://github.com/angular/angular.js/blob/ccd4227cfe2e12b8a9b7f6a7a18b210fd799677c/test/ng/directive/aSpec.js#L68\"相对=nofollow>间谍jQuery的,我会看到作为一个外部服务。

In the Angular Tests they use spies for spying on Jquery which I would see as an external service.

spyOn(jq.prototype, 'on');

<一个href=\"https://github.com/angular/angular.js/blob/6476aed7626fa7b4adefa99c5cb4a86ed371835c/test/ng/exceptionHandlerSpec.js\"相对=nofollow> $提供似乎是内部服务被使用了。

$provide seems to be used more for internal services.

  module(function($provide){
    $provide.provider('$exceptionHandler', $ExceptionHandlerProvider);
  });

还有一个茉莉花createSpy功能,但现在我在想,$提供应始终以precedence了这一点。

There is also a Jasmine createSpy function but now I'm thinking that $provide should always take precedence over that.

在这个任何见解或帮助将是AP preciated。

Any insights or help in this would be appreciated.

推荐答案

从我自己的(有限)的经验,我会说尽一切办法使:

From my own (limited) experience, I would say do whatever approach makes:


  • 测试code简单/更清晰/短

  • 限制什么的code你的测试内部做的假设。

  • 降低其副作用的影响(如实际运行Ajax请求)

  • 保持试验尽可能短,在字词或运行时间。

通常 spyOn 办法工作时,为了做到上述情况,我想从存根服务/工厂一个方法。如果我需要模拟一个完整的服务/工厂,然后用 $提供

Usually the spyOn approach works when, in order to do the above, I would like to stub a single method from a service / factory. If I need to mock an entire service / factory, then use $provide.

一个少数特定情况下会想到需要一个或另一个:

A few specific cases come to mind that require one or the other:


  • 如果你正在测试一项服务,然后存根从该服务的其他方法,你将不得不使用 spyOn

要确保额外的依赖并不被测后来在code出台,比 $提供更增添了一点保障。也就是说,如果你想确保 ServiceA 只要求 myMethod的 ServiceB ,然后 $提供我觉得这是要走的路,仿佛 ServiceA 要求从任何未定义的方法 ServiceB 在测试过程中,错误将得到提升。

To ensure that extra dependencies aren't introduced later in the code under test, than $provide adds a bit more protection. Say, if you want to ensure that ServiceA only requires myMethod from ServiceB, then $provide I think would be the way to go, as if ServiceA calls any undefined methods from ServiceB during the test, errors would be raised.

$provide.provider('ServiceB', {
    myMethod: function() {}
});


  • 如果你想嘲笑一个工厂,返回的函数,因此:

  • If you want to mock a factory that returns a function, so:

    app.factory('myFactory', function() {
      return function(option) {
        // Do something here
      }
    });
    

    哪个用作

    myFactory(option);
    

    然后,以验证某些code调用 myFactory(选购件)我觉得没有办法,然后使用 $提供来嘲弄工厂。

    Then to verify that some code calls myFactory(option) I think there is no alternative then to use $provide to mock the factory.

    只是顺便说说,他们不是互相排斥的选项。您可以使用 $提供然后还有涉及间谍。在previous例如,如果您想验证工厂被称为一个选项,您可能需要:

    Just by the way, they're not mutually-exclusive options. You can use $provide and then still have spies involved. In the previous example, if you want to verify the factory was called with an option, you might have to:

    var myFactorySpy = jasmine.createSpy();
    $provide.provider('myFactory', myFactorySpy);
    

    和随后在适当的点上的测试

    And then in the test at the appropriate point:

    expect(myFactorySpy).toHaveBeenCalledWith(option);
    

    这篇关于什么时候应该使用$提供与在我的角度JS单元测试茉莉花间谍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆