[英]Rookie error while writing test with Chai, Mocha, Express, johnny-five and node
Hi there I'm trying to learn a bit of test driven development using express, mocha, chai and johnny-five.嗨,我正在尝试使用 express、mocha、chai 和 johnny-5 来学习一些测试驱动开发。 So I wrote this little application that can turn an LED on and off.所以我写了这个可以打开和关闭 LED 的小应用程序。 The application works but my test fails.该应用程序有效,但我的测试失败。 Can somebody tell me what I am doing wrong in my test?有人可以告诉我我在测试中做错了什么吗?
Thank you谢谢
The output of npm test
is npm test
的输出是
> blink@1.0.0 test /Users/me/Documents/johnny-five/blink
> mocha --reporter spec
1435257445439 Looking for connected device
j5
.on()
1) Should turn a led on
.off()
✓ Should turn a led off
1 passing (13ms)
1 failing
1) j5 .on() Should turn a led on:
AssertionError: expected undefined to equal 1
at Context.<anonymous> (test/j5.js:9:14)
npm ERR! Test failed. See above for more details.
This is test/j5.js这是 test/j5.js
require('mocha');
var assert = require('chai').assert;
var j5 = require("../j5");
describe('j5', function () {
describe('.on()', function () {
it('Should turn a led on',function(){
var result = j5.on();
assert.equal(result, 1);
});
});
describe('.off()', function () {
it('Should turn a led off', function () {
// var res = j5.on();
// expect(res).to.equal(0);
});
});
});
This is server.js这是 server.js
var express = require("express");
var app = express();
var j5 = require("./j5");
var port = 3000;
app.get('/', function(req, res){
res.send('hello j5');
});
app.get("/on", function(req, res) {
j5.on();
res.send("on");
});
app.get("/off", function(req, res) {
j5.off();
res.send("off");
});
console.log("listening on port http://localhost:" + port);
app.listen(3000);
This is j5.js这是 j5.js
var exports = module.exports = {};
var five = require("johnny-five");
var board = new five.Board();
var board_ready = false;
var led = null;
board.on("ready", function() {
board_ready = true;
led = new five.Led(13);
});
exports.on = function() {
if (led !== null && board_ready === true) {
led.on();
return 1;
}
};
exports.off = function() {
if (led !== null && board_ready === true) {
led.off();
return 0;
}
};
EDIT: The path to my j5.js in test/j5.js was wrong.编辑:我在 test/j5.js 中的 j5.js 的路径是错误的。 but now I have a new error.但现在我有一个新的错误。 AssertionError: expected undefined to equal 1 at Context.断言错误:在上下文中预期未定义等于 1。 (test/j5.js:9:14). (测试/j5.js:9:14)。
After some playing around I found my error.经过一番玩耍后,我发现了我的错误。
johnny-five needs some time to connect to the board via the serial. johnny-5 需要一些时间才能通过串口连接到开发板。 As soon as the build in REPL is available I can use the functions on()
and off()
.一旦 REPL 中的构建可用,我就可以使用on()
和off()
函数。 So I made my test wait for 5 seconds before making the call of j5.on()
.所以我让我的测试在调用j5.on()
之前等待 5 秒钟。 The standard max timeout for the done()
function is 2000ms. done()
函数的标准最大超时为 2000 毫秒。 To make this longer I used this.timeout(10000);
为了延长这个时间,我使用了this.timeout(10000);
This is my new test/j5.js这是我的新测试/j5.js
require('mocha');
var assert = require('chai').assert;
var j5 = require("../j5");
var result = null;
describe('j5', function() {
describe('.on()', function() {
it('Should turn a led on', function(done) {
this.timeout(10000);
setTimeout(function() {
result = j5.on();
assert.equal(result, 1);
done();
}, 5000);
});
});
});
Result of npm test
: npm test
结果:
> blink@1.0.0 test /Users/icke/Documents/johnny-five/blink
> mocha --reporter spec
1435305595110 Device(s) /dev/cu.usbmodem1421
1435305595124 Connected /dev/cu.usbmodem1421
j5
.on()
1435305598694 Repl Initialized
✓ Should turn a led on (5003ms)
.off()
✓ Should turn a led off
2 passing (5s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.