Reputation: 11064
Writing my first node.js test for my first gulp-plugin....using mocha and chai. I get TypeError: Cannot read property 'Assertion' of undefined
for ar errorCheckArg = spy(errorCheckArg);
. It seems the errorCheckArg
function is not available in the testing enviroment(I did a console.log(errorCheckArg)
and it showed undefined
).
Any idea what I am doing wrong?
describe('gulp-foobar', function() {
var fakeFile;
beforeEach(function() {
var fakeFile = new File({
contents: new Buffer('abufferwiththiscontent')
});
});
describe('get files', function() {
it('should do something', function(done) {
var foo = function() { };
foobar(foo);
var errorCheckArg = spy(errorCheckArg);
expect(errorCheckArg).to.have.been.called.with(arguments);
done();
});
});
});
The Gulp plugin/node script being tested:
function foorbar(callback) {
var destinationFile;
errorCheckArg(arguments);
return through2.obj(function(file, enc, next) {
...
Upvotes: 0
Views: 626
Reputation: 22553
If errorCheckArg is a function inside of foobar, you won't be able to spy on it unless you somehow expose it to the outside world. If it looks like so:
function foobar(callback) {
errorCheckArg(arguments);
.
.
.
function errorCheckArg(args){};
}
you've made it a private function.
If it's important enough to test it in in isolation, then you'll need to expose it somehow, and you'll need to refactor your code.
Without refactoring, your option would be to test errorCheckArg based on its effects on the outputs of foobar. If it were important to me to keep the idiom of just calling a function, that's what I would do.
Upvotes: 2