I have the following issue. I'm trying to stub 2 methods inside a module from my tests. Here's an example:
// module_to_test.js
var a = function(cb) {
console.log("a");
b(function(err) {
console.log("cb b", err);
if (err) { return cb(err); }
cb();
});
};
var b = function(cb) {
console.log("b");
cb('ErrB');
};
exports.a = a;
exports.b = b;
// test.js
var assert = require('assert');
var simple = require('simple-mock');
var proxyquire = require('proxyquire');
describe ("#test", function() {
afterEach(function() {
simple.restore();
});
it ('test', function(done) {
var cadet = proxyquire('../cadet', { });
simple.mock(cadet, 'b').callbackWith(null);
cadet.a(function(err) {
assert.equal(err, null);
done();
});
});
});
As b() is stubbed and calling the callback with null, a() should call the callback with null too, thus the assert must be OK.
Turns out that the original b() function is being called, so 'ErrB' is passed to the callback function at a(), and a() is calling the callback with 'ErrB', so the assert is failing.
Now, if I add:
simple.mock(cadet, 'a').callbackWith(null);
(stub the same method I'm testing), it works OK. But certainly this is not what I want to do.
I've tried using sinon, but the same happens.
Any ideas? Thanks!
Aucun commentaire:
Enregistrer un commentaire