繁体   English   中英

使用 Chai、Mocha、Express、johnny-5 和 node 编写测试时出现新手错误

[英]Rookie error while writing test with Chai, Mocha, Express, johnny-five and node

嗨,我正在尝试使用 express、mocha、chai 和 johnny-5 来学习一些测试驱动开发。 所以我写了这个可以打开和关闭 LED 的小应用程序。 该应用程序有效,但我的测试失败。 有人可以告诉我我在测试中做错了什么吗?
谢谢

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.

这是 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);
    });
  });
});

这是 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);

这是 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;
  }
};

编辑:我在 test/j5.js 中的 j5.js 的路径是错误的。 但现在我有一个新的错误。 断言错误:在上下文中预期未定义等于 1。 (测试/j5.js:9:14)。

经过一番玩耍后,我发现了我的错误。
johnny-5 需要一些时间才能通过串口连接到开发板。 一旦 REPL 中的构建可用,我就可以使用on()off()函数。 所以我让我的测试在调用j5.on()之前等待 5 秒钟。 done()函数的标准最大超时为 2000 毫秒。 为了延长这个时间,我使用了this.timeout(10000);

这是我的新测试/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);
    });
  });
});

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.

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