我具有用于创建用户的辅助功能
const userCreate = (res, req, next, User) => {
console.log('REQ.BODY: ', req.body); // Log1
User.create(req.body)
.then((newUser) => {
const result = createJWT(newUser);
req.user = result;
console.log('REQ.USER: ', req.user); // Log2
next();
});
};
然后我编写测试:
describe('userCreate', () => {
it('it must return anything', () => {
const req = {
body: {
email: 'test@test.com',
password: 'Test12345',
confirm: 'Test12345',
username: 'Test',
},
user: sinon.spy(),
};
const res = {};
const next = sinon.spy();
authMiddleware.userCreate(res, req, next, User);
expect(req.user.calledWith({ email: 'test@test.com' })).to.be.ok; // Attempt1
expect(next.calledOnce).to.equal(true); // Attempt2
});
});
Log1始终具有数据。
Log2不起作用
我要检查req.user是否包含我的电子邮件Attempt1
下一个被称为Attempt2
在测试之外,此功能有效。