本文介绍了在 jasmine 中监视 jQuery $('...') 选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要监视 jQuery 函数(例如 bindclick 等),这很容易:

When it comes to spying on jQuery functions (e.g. bind, click, etc) it is easy:

spyOn($.fn, "bind");

问题是当你想监视 $('...') 并返回定义的元素数组时.

The problem is when you want to spy on $('...') and return defined array of elements.

在阅读 SO 上的其他相关答案后尝试的事情:

Things tried after reading other related answers on SO:

spyOn($.fn, "init").andReturn(elements); // works, but breaks stuff that uses jQuery selectors in afterEach(), etc
spyOn($.fn, "merge").andReturn(elements); // merge function doesn't seem to exist in jQuery 1.9.1
spyOn($.fn, "val").andReturn(elements); // function never gets called

那么我该怎么做呢?或者,如果唯一的方法是监视 init 函数,我如何在完成后从函数中删除"间谍,以便 afterEach() 路由不会中断.

So how do I do this? Or if the only way is to spy on init function how do I "remove" spy from function when I'm done so afterEach() routing doesn't break.

jQuery 版本是 1.9.1.

jQuery version is 1.9.1.

解决方法:

到目前为止,我可以让它工作的唯一方法(丑陋):

The only way I could make it work so far (ugly):

realDollar = $;
try {
  $ = jasmine.createSpy("dollar").andReturn(elements);
  // test code and asserts go here
} finally {
  $ = realDollar;
}

推荐答案

通常,间谍存在于规范的整个生命周期中.然而,消灭间谍并没有什么特别之处.您只需恢复原始函数引用即可.

Normally, a spy exists for the lifetime of the spec. However, there's nothing special about destroying a spy. You just restore the original function reference and that's that.

这是一个方便的小辅助函数(带有一个测试用例),它将清理您的解决方法并使其更可用.在你的 afterEach 中调用 unspy 方法来恢复原始引用.

Here's a handy little helper function (with a test case) that will clean up your workaround and make it more usable. Call the unspy method in your afterEach to restore the original reference.

function spyOn(obj, methodName) {
    var original = obj[methodName];
    var spy = jasmine.getEnv().spyOn(obj, methodName);
    spy.unspy = function () {
        if (original) {
            obj[methodName] = original;
            original = null;
        }
    };
    return spy;
}

describe("unspy", function () {
    it("removes the spy", function () {
        var mockDiv = document.createElement("div");
        var mockResult = $(mockDiv);

        spyOn(window, "$").and.returnValue(mockResult);
        expect($(document.body).get(0)).toBe(mockDiv);

        $.unspy();

        expect(jasmine.isSpy($)).toEqual(false);
        expect($(document.body).get(0)).toBe(document.body);
    });
});

作为上述方法的替代方案(以及其他阅读本文的人),您可以改变处理问题的方式.不要监视 $ 函数,而是尝试将对 $ 的原始调用提取到它自己的方法中,然后监视它.

As an alternative to the above (and for anyone else reading this), you could change the way you're approaching the problem. Instead of spying on the $ function, try extracting the original call to $ to its own method and spying on that instead.

// Original
myObj.doStuff = function () {
    $("#someElement").css("color", "red");
};

// Becomes...
myObj.doStuff = function () {
    this.getElements().css("color", "red");
};

myObj.getElements = function () {
    return $("#someElement");
};

// Test case
it("does stuff", function () {
    spyOn(myObj, "getElements").and.returnValue($(/* mock elements */));
    // ...
});

这篇关于在 jasmine 中监视 jQuery $('...') 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 07:22