javascript 类型错误:'undefined' 不是一个函数(评估 'sinon.spy()')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20570301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
提问by Sergey
I'm trying to use sinon.jsin testing of a backbone application. But unfortunately I cannot use spy method due to error:
我正在尝试使用sinon.js来测试主干应用程序。但不幸的是,由于错误,我无法使用 spy 方法:
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Here is the steps to reproduce the error:
以下是重现错误的步骤:
- Create an empty project with backbone yeoman generator
- Install sinon:
cd test && bower install sinon
- Include in test/index.html
<script src="bower_components/sinon/lib/sinon.js"></script>
Create spy in test/spec/test.js:
describe('Give it some context', function () { describe('maybe a bit more context here', function () { it('should run here few assertions', function () { var spy = sinon.spy(); spy.should.be.ok; }); }); });
Run the test with grunt:
grunt test
The test will fail with a described error.
- 使用主干自耕农生成器创建一个空项目
- 安装 sinon:
cd test && bower install sinon
- 包含在 test/index.html 中
<script src="bower_components/sinon/lib/sinon.js"></script>
在 test/spec/test.js 中创建间谍:
describe('Give it some context', function () { describe('maybe a bit more context here', function () { it('should run here few assertions', function () { var spy = sinon.spy(); spy.should.be.ok; }); }); });
用 grunt 运行测试:
grunt test
测试将失败并显示所描述的错误。
Could anyone help to find out what is wrong?
任何人都可以帮助找出问题所在吗?
采纳答案by Sergey
It turned out that such functionality as spies, stubs, etc should be added manually by including scripts from lib/sinon
folder. This fact is mentioned in Installation section. And due to the code of the core sinon.js file only in Node.js environment it is done automatically.
事实证明,应该通过包含lib/sinon
文件夹中的脚本手动添加间谍、存根等功能。安装部分提到了这一事实。并且由于核心 sinon.js 文件的代码仅在 Node.js 环境中自动完成。
回答by Kamil Szot
I'll just leave here the list of files that sinon
conveniently forgets to load if it is loaded as a <script>
or with require.js
(as AMD module) - basically anything else than in node.js
:
sinon
如果它作为 a<script>
或 with require.js
(作为 AMD 模块)加载,我将在这里留下方便忘记加载的文件列表- 基本上除了 in 之外的任何其他内容node.js
:
"sinon/lib/sinon.js",
"sinon/lib/sinon/spy.js",
"sinon/lib/sinon/call.js",
"sinon/lib/sinon/behavior.js",
"sinon/lib/sinon/stub.js",
"sinon/lib/sinon/mock.js",
"sinon/lib/sinon/collection.js",
"sinon/lib/sinon/assert.js",
"sinon/lib/sinon/sandbox.js",
"sinon/lib/sinon/test.js",
"sinon/lib/sinon/test_case.js",
"sinon/lib/sinon/match.js"
Feel free to skip any of those but expect sinon to fail in curious ways.
随意跳过其中任何一个,但希望 sinon 会以奇怪的方式失败。
回答by Jason
I encountered the same problem with sinon 1.17.2 and Chrome 47.0. After trying the above solutions and variations of those, I ended up using the nuclear option and switching to Jasmine.
我在 sinon 1.17.2 和 Chrome 47.0 上遇到了同样的问题。在尝试了上述解决方案和这些解决方案的变体之后,我最终使用了核选项并切换到了Jasmine。
For my test suite, it only took about 15 minutes of some global find-and-replace to convert my chai 'expects' into Jasmine ones and some differences around mocha before syntax; Jasmine flagged the unexpected syntax clearly. Jasmine spy objectswere a fine substitute for sinon.
对于我的测试套件,只需要大约 15 分钟的一些全局查找和替换就将我的 chai '期望' 转换为 Jasmine 并且在语法之前围绕 mocha 的一些差异;Jasmine 清楚地标记了意外的语法。Jasmine 间谍物品是 sinon 的一个很好的替代品。
回答by Jason
Unlike the other answers, I didn't install simon manually by including each individual source file. Instead I followed the advice of How To Install Sinon.JS In The Browser With Bower.
与其他答案不同,我没有通过包含每个单独的源文件来手动安装 simon。相反,我遵循了如何使用 Bower 在浏览器中安装 Sinon.JS的建议。
bower install http://sinonjs.org/releases/sinon-1.17.6.js
then
然后
bower list -p
'sinon-1.17.6': 'bower_components/sinon-1.17.6/index.js'
And
和
<script src="bower_components/sinon-1.17.6/index.js"></script>
Worked for me.
对我来说有效。