0
我有一些控制器/路由用來處理用戶註冊:如何模擬一個import express.js supertest?
controllers/user.js
const User = require('../models/User'); // What I want to mock!
...
/**
* POST /signup
* Create a new local account.
*/
exports.postSignup = (req, res, next) => {
...
const user = new User({
email: req.body.email,
password: req.body.password
});
...
我想測試User
時調用正確的ARGS。
test/userIntegrationTest.js
const request = require('supertest');
const app = require('../app.js');
const sinon = require('sinon');
const User = require('../models/User');
describe('POST /signup',() => {
it('should create user with valid email', (done) => {
const formdata = {email: '[email protected]', password: 'asdf'};
const UserMock = sinon.mock(User); // This mock doesn't do what I want!
request(app)
.post('/signup')
.send(formdata)
.expect(200)
.end(function(res) {
expect(UserMock.calledOnce).to.be.true;
expect(UserMock.calledWith(formdata));
})
});
});
我希望UserMock
從控制器被稱爲然而,這模擬的似乎只有嘲笑User
模式在userIntegrationTest.js
進口。
如何模擬controllers/user.js
中導入的User
。