0
我正在使用mocha和sinon進行nodejs單元測試。我有以下監視nodejs單元測試中的依賴關係
users.js
const Database = require('./lib/Database');
exports.setupNewUser = (name) => {
var user = {
name: name
};
try {
Database.save(user);
}
catch(err) {
console.error('something failed');
}
}
Database.js
exports.save = (user) => {
console.log(`saving: ${user}`);
};
userTest.js
const sinon = require('sinon');
require('chai').should();
const users = require('../src/users');
describe('users',() => {
it('should log an error when the Database save fails',() => {
var databaseSpy = sinon.spy(Database, 'save').throws(); // this is supposed to work??
users.setupNewUser('Charles');
databaseSpy.should.be.called;
});
});
加ording to the sinon tutorials我讀過了,我應該可以創建databaseSpy
但我不斷收到此錯誤:ReferenceError:Database is not defined
我在想什麼?