用Jasmine模拟$ .fn.find似乎有魔术行为

时间:2014-06-03 15:06:37

标签: javascript jquery tdd jasmine

我正在编写一个方法,它是对$ .fn.modal调用的简单包装。该方法需要一个选择器,并且该选择器应该传递给jQuery,因此jQuery可以找到与该选择器匹配的元素。

以下是我的规范:

describe('The modal presenter', function() {

  it('operates on the provided selector', function() {
    spyOn($.fn, 'find');
    myModule.presentModal('#the-modal', {});
    expect($.fn.find).toHaveBeenCalledWith('#the-modal');
  });

});

这一切都很好。奇怪的行为发生在我的生产代码中。

var myModule = {
  presentModal: function(selector, event) {
    event.preventDefault();
    $(selector, document).modal('show'); // This line works
    $(document).find(selector).modal('show'); // This line doesn’t work?!

    // This should be fairly obvious, but I’m not running both
    // of the above lines at the same time. I comment one out
    // and run the method with one line at a time.
  }
}

据我所知,我添加评论的那两行应该是等价的。他们应该使用$.fn.find致电selector。第二个注释行应肯定调用.find,因为我已经明确地写过。

当我进行测试时,我得到TypeError: $(...).find(...) is undefined。怎么会这样?

0 个答案:

没有答案