尝试设置状态代码时,res.status不是一个函数

时间:2017-11-27 01:34:17

标签: javascript node.js mocha sinon

当试图满足由提供的单元测试设置的规范时,当试图返回方法的状态代码时,我被

命中
  

TypeError:res.status不是函数

在API实现中运行createUser函数时。它发生在每个方法调用中,例如res.send,res.sendStatus等。即使我将res.status()添加到测试中以将其设置在那里,它也会返回相同的错误。

apiTests.js

let chai = require('chai');
let expect = chai.expect;
let sinon = require('sinon');
let sinonChai = require('sinon-chai');
chai.use(sinonChai);

let model = require('../tron_model.js'); // for stubbing only
let api = require('../tron_api.js');

describe('API', function() {
    describe('creating users', function() {
        it('should return 201 on creating user', function () {
            let mockModel = sinon.stub(new model.TronModel());
            mockModel.addUser.onFirstCall().returns(new model.User('user','pass'));
            let req = {body: {username: 'goatzilla', password: 'x'}};
            let res = {end: function(){}};
            api.init(mockModel);
            api.createUser(req, res);
            expect(res.statusCode).to.equal(201);
            expect(mockModel.addUser).to.have.callCount(1);
        });
    });
});

tron_api.js

let model = undefined;
let api = exports;

api.init = function(modelArg) {
    model = modelArg;
};

api.createUser = function(req, res) {
    model.addUser(req.body.username, req.body.password);
    console.log(res);
    res.status(201);
};

0 个答案:

没有答案