如何使用Sinon.JS 删除google.maps 库?

发布于 2024-12-18 23:20:30 字数 294 浏览 1 评论 0原文

我在 Backbone 模型中使用 google.maps 库,如下所示(coffeescript):

class Route extends Backbone.Model

  initialize: ->
    @directionsService = new google.maps.DirectionsService()

在我的测试中,每当我尝试实例化 Route 时,我显然都会遇到问题。如何在测试中删除 google 以免导致此问题?

I'm using the google.maps library inside a Backbone model like this (coffeescript):

class Route extends Backbone.Model

  initialize: ->
    @directionsService = new google.maps.DirectionsService()

In my tests, I whenever I try to instantiate a Route, I obviously run in to an issue. How can I stub out google in my test so that it won't cause this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡风轻 2024-12-25 23:20:30

对 coffescript 不太了解,但您可以为模型构造函数提供第二个对象作为参数。

var mymodel = new Route({/*attributes*/}, {directionService: yourStub});

然后在初始化函数中您可以编写:

initialize: function(atts, options) {
  this.directionService = options.directionService || new google.maps.DirectionsService();
}

现在您可以存根方向服务或使用另一个服务(如果有)用于单个实例。

另一种方法是直接替换 DirectionService:

var origService = google.maps.DirectionsService;
google.maps.DirectionsService = function() {/*your stub*/};
var route = new Route();
google.maps.DirectionsService = origService;

Don't know much about coffescript, but you can give the model constructor a second object as argument.

var mymodel = new Route({/*attributes*/}, {directionService: yourStub});

Then in the initialize function you would write:

initialize: function(atts, options) {
  this.directionService = options.directionService || new google.maps.DirectionsService();
}

Now you can stub the direction service or use another one (if there is any) for single instances.

Another way would be to replace the DirectionService directly:

var origService = google.maps.DirectionsService;
google.maps.DirectionsService = function() {/*your stub*/};
var route = new Route();
google.maps.DirectionsService = origService;
海螺姑娘 2024-12-25 23:20:30

当您尝试编写可测试代码时,主要的失败之一是在要测试的对象中创建新实例。有一种调用控制反转的模式有助于编写可测试的代码。诀窍在于,您在类中创建的所有内容都将被注入到构造函数中。通过这种方式,在测试中您只需注入一个简单的模拟或存根即可。所以ProTom的回答就是关于这个模式的。

另一个解决方案:
在 JavaScript 中,我们可以轻松地重写我们自己的每个对象/函数。这意味着您可以创建自己的 google.map DirectionsService。顺便说一句,最好在不依赖其他库的情况下测试您的代码,因此您应该使用所需的方法创建自己的 google 对象。

One of the main failure when you try to write testable code is to create new instances in your object you want to test. There is a pattern calling Inversion of control that helps to write testable code. The trick is that all the stuff you would create in your class will be injected into the constructor. Doing it this way, in your test you can just inject a simple mock or stub. So the answer of ProTom is about this pattern.

Another solution:
In JavaScript we can easily override every object/function by our own. Which means you can create your own google.map DirectionsService. Btw it would better to test your code without any dependencies to other libs, so you should create your own google object with the methods you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文