错误:预期200“ OK”,通过Mocha得到404“ Not Found”

时间:2018-08-27 19:18:27

标签: node.js mocha

我正在关注inLearning上的教程“ Node.js:测试驱动开发”,完成该教程后,我仍然会收到:

user data
✓ should have +1 user count after saving
user web
1) should have +1 user count after saving


1 passing (67ms)
1 failing

1) user web
   should have +1 user count after saving:
Error: expected 200 "OK", got 404 "Not Found"
  at Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
  at Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
  at Test.assert (node_modules/supertest/lib/test.js:173:18)
  at assert (node_modules/supertest/lib/test.js:131:12)
  at /home/ubuntu/workspace/TestDrivenDevelopment/node_modules/supertest/lib/test.js:128:5
  at Test.Request.callback (node_modules/superagent/lib/node/index.js:718:3)
  at IncomingMessage.parser (node_modules/superagent/lib/node/index.js:906:18)
  at endReadableNT (_stream_readable.js:974:12)
  at _combinedTickCallback (internal/process/next_tick.js:80:11)
  at process._tickCallback (internal/process/next_tick.js:104:9)

使用SuperTest框架,我们正在测试用户Web API。我检查了node_modules目录,并确保同时存在supertest和co-supertest。

我将代码与本教程进行了比较,除了

var app = module.exports = koa();

自2015年以来发生了变化,因此必须包含新的

var app = module.exports = new koa();

可能是什么原因造成的?

共有四个文件。

user-api-spec.js

require('co-mocha');
var should = require('should');
var data = require('../user-data.js');
var fs = require('co-fs');
var api = require('../user-web.js');
var request = require('co-supertest').agent(api.listen());

before(function* (){
 yield fs.writeFile('./users.json', '[]');
});

describe("user data", function(){
   it("should have +1 user count after saving", function* (){
     var users = yield data.users.get();

     yield data.users.save({name:'John'});

     var newUsers = yield data.users.get();

     newUsers.length.should.equal(users.length + 1);
 });
});

//USER WEB TEST w/ SUPERTEST FRAMEWORK
describe('user web', function(){
 it("should have +1 user count after saving", function* (){
     var users = (yield request.get('/user').expect(200).end()).body;

     yield data.users.save({name:'John'});

     var newUsers = (yield request.get('/user').expect(200).end()).body;

     newUsers.length.should.equal(users.length + 1);
 });
});

user-web.js

var koa = require('koa');
var router = require('koa-router')();

var app = module.exports = new koa();

var data = require('./user-data.js');

router.get('/user', function* (){
 this.body = yield data.users.get();
});

app.use(router.routes());

app.listen(3000);

user-data.js

var fs = require('co-fs');

var userFile = './users.json';

module.exports = {
 users : {
    get: function *() {
        var data = yield fs.readFile(userFile, 'utf-8');
        return JSON.parse(data);
    },
    save: function *(user) {
        var users = yield this.get();

        users.push(user);

        yield fs.writeFile(userFile, JSON.stringify(users));
    }
  }
}

users.json

[{"name":"John"}]

1 个答案:

答案 0 :(得分:0)

状态404表示,到目前为止,您的Mocha测试用例请求尚未与主服务器连接,这可能是在其他地方。还是检查您的package-json中是否提到了test:'mocha'作为命令?然后

我建议您创建一个简单的get api并返回一些值,然后使用mocha和控制台进行测试。