简体   繁体   中英

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. So I wrote this little application that can turn an LED on and off. 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

> 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

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

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

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. but now I have a new error. AssertionError: expected undefined to equal 1 at Context. (test/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. As soon as the build in REPL is available I can use the functions on() and off() . So I made my test wait for 5 seconds before making the call of j5.on() . The standard max timeout for the done() function is 2000ms. To make this longer I used this.timeout(10000);

This is my new test/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 :

> 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM