Mocha、Sinon、および SuperTest を使用したかなり単純なテストがあります。
describe('POST /account/register', function(done) {
beforeEach(function(done) {
this.accountToPost = {
firstName: 'Alex',
lastName: 'Brown',
email: 'a@b.com',
password: 'password123'
};
this.email = require('../../app/helpers/email');
sinon.stub(this.email, 'sendOne')
done();
});
afterEach(function(done){
this.email.sendOne.restore();
done();
})
it('sends welcome email', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
//this line is failing
assert(this.email.sendOne.calledWith('welcome'));
done();
});
});
});
私が抱えている問題は、assert(this.email.sendOne.calledWith('welcome'))
this.email が定義されていないときです。
これは、私が期待していたスコープではないためだと確信しています。これは現在、request.end のスコープになっていると思います。
関数が呼び出されたことをアサートするために、sinon スタブにアクセスするにはどうすればよいですか?