简体   繁体   English

我如何使用Sinon存根$(window).width()?

[英]How can i stub $(window).width() using Sinon?

I have a function in JS view which performs some action if the window width is less than 1000. I'm attempting to write unit tests for this with Mocha, chai and running the tests via karma test runner in Phantom/Chrome/Chromium browser. 我在JS视图中有一个函数,如果窗口宽度小于1000,它会执行一些操作。我正在尝试使用Mocha,chai为此编写单元测试,并通过Phantom / Chrome / Chromium浏览器中的karma测试运行器运行测试。

I also use sinon to stub the methods and making it to return some desired value. 我还使用sinon来保存方法并使其返回一些所需的值。 Now there is a condition check where if the window width is less than 1000 so how can i stub this, i was trying something like below, 现在有一个条件检查,如果窗口宽度小于1000所以我怎么能存根,我尝试下面的东西,

sinon.stub($(window).width());
$(window).width().returns(900);

But it is not working. 但它没有用。 Is there any specific way where i can stub $(window).width() value? 有没有特定的方式我可以存根$(window).width()值?

sinon.stub() usage: sinon.stub()用法:

First: you're not passing methods/functions to sinon.stub() correctly. 首先:你没有正确地将方法/函数传递给sinon.stub() Here's the correct way to do it: 这是正确的方法:

sinon.stub(object, 'method');

Where object is an object whose method you want to stub, 'method' is a string containing the name of this method. object是您想要存根的对象的对象, 'method'是包含此方法名称的字符串。

Another way is to simply overwrite current function with a stub, calling .stub() with no arguments: 另一种方法是简单地用存根覆盖当前函数,调用没有参数的.stub()

object.method = sinon.stub();

Taking that into account, let's move on to your current code. 考虑到这一点,让我们继续您当前的代码。

Current code: 当前代码:

sinon.stub($(window).width());

As I wrote above - this is an incorrect call to sinon.stub() . 正如我上面所写 - 这是对sinon.stub()的错误调用。

Putting that aside, you're approaching it from the wrong angle. 把它放在一边,你是从错误的角度接近它。 You cannot stub $(window).width() - $(window).width() isn't a function , it's a call to a function - returning a number (here: width of the window in px). 你不能存根$(window).width() - $(window).width()是不是一个功能 ,它是一个函数的调用-返回一个数字 (这里:在PX窗口的宽度)。 That's the value you would actually be trying to replace with a stub - arbitrary primitive (number), not even bound to any variable. 这是你实际上试图用存根替换的值 - 任意原语(数字),甚至不绑定到任何变量。

Next try: 接下来尝试:

sinon.stub($(window), 'width');

Now, why this wouldn't work ? 现在,为什么不起作用

We're only stubbing width method on this specific jQuery object - but each call to $(...) creates a new one. 我们只是在这个特定的 jQuery对象上存根width方法 - 每次调用$(...)都会创建一个新的。 Here's the result: 这是结果:

var $window = $(window);
sinon.stub($window, 'width').returns(900);

$window.width(); // 900 - using .width() method stub
$(window).width(); // real window width - new jQuery object

(jsfiddle: http://jsfiddle.net/xqco8u77/ ) (jsfiddle: http//jsfiddle.net/xqco8u77/

Solution

Stub the .width() method on prototype of jQuery objects - whose methods/values are available globally for each jQuery object. 在jQuery对象的prototype.width()方法 - 其方法/值对于每个jQuery对象是全局可用的。

sinon.stub($.prototype, 'width').returns(900);

// "900"
$(window).width();

// restoring original function
$.prototype.width.restore();

// real window width
$(window).width();

Working demo on jsfiddle: http://jsfiddle.net/gjcguvzh/5/ 关于jsfiddle的工作演示: http//jsfiddle.net/gjcguvzh/5/

(As you can see, I've also restored original .width() prototype method after using the stub, in case you have to make use of it later.) (正如您所看到的,我在使用存根之后还恢复了原始.width()原型方法,以防您以后必须使用它。)

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

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