Sinon找不到方法'间谍'

Sinon cannot find method 'spy'

本文关键字:间谍 找不到 方法 Sinon      更新时间:2023-09-26

我正努力在骨干应用程序中使用require.js/mocha/chai/sinon,以提升学习曲线。当我运行此测试时:

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();
            });
        });
    });
});

我在定义helloSpy的行上得到TypeError: Object #<Object> has no method 'spy'。为什么?请注意,第一个测试通过。

以下是完整的项目:

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

注意使用特定的承诺。

这里的问题是sinon的bower存储库无法使用。Sinon必须先建,做bower install sinon只会拉低Sinon.JS回购。使用bower install sinonjs而不是bower install sinon可以工作,但提供了早期版本号。

来自@Erik链接。

install --save-dev sinonjs-built

这将使您构建sinon版本。

编辑

另一个bower版本(正如@Erik上面建议的)可能在https://github.com/blittle/sinon.js

可以通过安装install --save-dev sinonjs

编辑2

来自sinon github:

重要提示:AMD需要预构建版本Sinon.JS作为源代码不适用于AMD加载程序(当它们是异步的,比如通过浏览器中的脚本标签加载)。为此,您必须使用预构建版本。您可以自己构建它,也可以从http://sinonjs.org.

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

您可以编辑bower.json文件。而不是写入版本,只为CCD_ 10传递CCD_

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