TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')

▼魔方 西西 提交于 2019-12-04 22:44:34
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/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.

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.

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.

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 objects were a fine substitute for sinon.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!