describe('Ajax', function () {
beforeEach(function () {
// Instantiate module and reference it with this.testUser
this.testUser = new TestUser();
// Reference sinon.spy with this.spySetToken
this.spySetToken = sinon.spy(this.testUser, 'setToken');
});
afterEach(function () {
this.spySetToken.restore();
});
it('Does it respond with that data', function () {
// Wrap $.ajax method and invoke success callback from ajax passing it a 'string'.
sinon.stub($, 'ajax').yieldsTo('success', 'Custom response string');
// test to see if my method that's inside the success callback is called with the string
expect(this.spySetToken.toHaveBeenCalledWith('Custom response string');
});
});
「呼び出されると予想される関数」が表示されます。
Ajax 成功メソッドを正常にテストするにはどうすればよいですか?