I am creating a simple test in Mocha to see if the command-line arguments are being passed properly. I'm using "child_process" to access the stdout in buffer but mocha gives me the error that my stdout is undefined.
Here is the error:
main function
1) should take argument for greetings
2) should take argunent for goodbye
2 passing (19ms)
2 failing
1) main function should take argument for greetings:
AssertionError: expected 'hello undefined' to equal 'hello Rahul'
+ expected - actual
+hello Rahul
-hello undefined
at Context.<anonymous> (/Users/rahulsharma/Desktop/CF-HW/simpltest/test/main_test.js:14:40)
at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:452:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:309:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
2) main function should take argunent for goodbye:
AssertionError: expected 'goodbye undefined' to equal 'goodbye Rahul'
+ expected - actual
+goodbye Rahul
-goodbye undefined
at Context.<anonymous> (/Users/rahulsharma/Desktop/CF-HW/simpltest/test/main_test.js:25:38)
at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:452:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:309:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
And here is the test file:
var expect = require('chai').expect;
var main = require('../lib/main');
describe('main function', function (){
it('should take argument for greetings', function() {
var exec = require('child_process').exec;
var name;
exec('Rahul', function (error, stdout, stderr){
name = stdout;
});
expect(main.greetings(name)).to.equal('hello Rahul');
});
it('should take argunent for goodbye', function() {
var exec = require('child_process').exec;
var name;
exec('Rahul', function (error, stdout, stderr){
name = stdout;
});
expect(main.goodbye(name)).to.equal('goodbye Rahul');
});
});
And file being tested:
var greetme = require('./greet.js');
greetme.greetings('Rahul');
greetme.goodbye('Rahul');
exports.greetings = function(name) {
this.name = name;
return ('hello ' + this.name);
}
exports.goodbye = function(name) {
this.name = name;
return ('goodbye ' + this.name);
}
process.argv.forEach(function(x){
greetme.greetings(x);
});
process.argv.forEach(function(x){
greetme.goodbye(x);
});
Any ideas on what's happening here?
Aucun commentaire:
Enregistrer un commentaire