【问题标题】:Sinon cannot find method 'spy'诗乃找不到方法'spy'
【发布时间】:2013-08-25 19:47:23
【问题描述】:

我正在尝试在将 require.js/mocha/chai/sinon 与主干应用程序一起使用时攀登学习曲线。当我运行this test

define([
    "chai",
    "sinon"
], function(chai, sinon){
    var expect = chai.expect;

    describe("Trying out the test libraries", function(){
        describe("Chai", function(){
            it("should be equal using 'expect'", function(){
                expect(hello()).to.equal("Hello World");
            });
        });

        describe("Sinon.JS", function(){
            it("should report spy called", function(){
                var helloSpy = sinon.spy(window, "hello");

                expect(helloSpy.called).to.be.false;
                hello();
                expect(helloSpy.called).to.be.true;
                hello.restore();
            });
        });
    });
});

我得到TypeError: Object #<Object> has no method 'spy' on the line where helloSpy is defined。为什么?请注意,第一个测试通过了。

这是完整的项目:

https://github.com/ErikEvenson/spa-testing-study/tree/bcc5b71b3b6f8b24f7e8d01673b50682498ee1b2.

小心使用那个特定的提交。

【问题讨论】:

  • 您确认sinon 是您所期望的吗?
  • 我做到了,但事实并非如此。它是一个对象,由 sinon.js 创建,但没有 spy() 方法。
  • 我会看看 require.js 的设置。我只是在墙上抛出一些想法,看看有什么坚持;抱歉,我不能更具体,我对这些工具不是很熟悉。
  • 我认为你是对的——我怀疑 sinon 无法找到 spy.js 所在的目录——但我不知道如何解决这个问题......跨度>
  • 有没有sinon.sinon.spy

标签: javascript backbone.js mocha.js sinon chai


【解决方案1】:

这里的问题原来是 sinon 的 bower 存储库无法使用,正如 this issue 一样。必须先构建 Sinon,然后执行 bower install sinon 只会拉下 Sinon.JS 存储库。使用 bower install sinonjs 而不是 bower install sinon 可以,但会提供更早的版本号。

【讨论】:

    【解决方案2】:

    来自@Erik 链接。

    install --save-dev sinonjs-built

    这将为您提供构建版本的 sinon。

    编辑

    可以在https://github.com/blittle/sinon.js 中找到另一个凉亭版本(如上面@Erik 建议的那样)

    可以通过以下方式安装它 install --save-dev sinonjs

    编辑 2

    来自sinon github

    重要提示:AMD 需要预建版本 作为源的 Sinon.JS 不适用于 AMD 加载器(当它们是异步的时,例如通过浏览器中的脚本标签加载)。为此,您将不得不使用预构建的版本。您可以自己构建它,也可以从http://sinonjs.org 获取编号版本。

    解决方法:告诉bower sinon文件的直接链接

    您可以编辑bower.json 文件。而不是编写版本,只需将url 传递给file

    [...]
    "devDependencies": {
         "chai": "~1.10.0",
        "sinon": "http://sinonjs.org/releases/sinon-1.12.2.js#*",
     },
    [...]
    

    【讨论】:

    • 这是第三方构建,未与最新的 sinon 版本保持同步。
    • @demisx,感谢更新链接到更好的凉亭版本.. 但我不知道它是否是最新的
    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 2019-07-07
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    相关资源
    最近更新 更多