问题
I'm using Jasmine for testing client & server side javascript and need to do some mocking. Has anyone got any suggestions for a good javascript mocking framework?
回答1:
I tried this once, but ended up refactoring instead so it wasn't needed. It doesn't have dependencies, so it should work just fine on node.js. http://sinonjs.org/
These are testing frameworks, but some of them include mocking frameworks internally: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
回答2:
I have not tried this one out(yet), but you could try node-gently created by Felix Geisendörfer. Right now I saw nodemock update at search.npmjs.org which is commited frequently also.
Quick search gave me:
- https://github.com/aikar/mock
- https://github.com/arunoda/nodemock
- https://github.com/dhasenan/maryjane
- https://github.com/cjohansen/Sinon.JS
- https://github.com/dominictarr/remap
回答3:
I'm using node-gently and it works great. Is not as complete as sinon but feels more confortable to work with.
回答4:
Why do you need a mocking framework? In statically-typed languages you need one because you can't change types at run-time.
I don't think you need this with JavaScript, the language is dynamic and powerful enough for you to not need this.
For example, I use backbone
for my models and for the database connectivity. Backbone was designed well and has a single method Backbone.Sync
that does all the database magic.
So I just do
var Backbone = require("backbone");
Backbone.Sync = function _mockedSync() {
return ...;
}
You need to clarify exactly what you need. I don't think you need a mocking framework.
来源:https://stackoverflow.com/questions/6679914/what-javascript-mocking-frameworks-are-people-using