测试ajax-jasminesinon
我得到了“期望调用的函数”测试ajax-jasminesinon,ajax,jasmine,sinon,Ajax,Jasmine,Sinon,我得到了“期望调用的函数” 如何成功地测试Ajax成功方法?我以前会使用sinon.fakeServer,但我没有意识到它触发了原始Ajax调用的成功 因此,解决方案是这样做: describe('Ajax', function () { beforeEach(function () { // Instantiate module and reference it with this.testUser this.testUser = new TestUser(); /
如何成功地测试Ajax成功方法?我以前会使用sinon.fakeServer,但我没有意识到它触发了原始Ajax调用的成功 因此,解决方案是这样做:
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');
});
});
Sinon.server将触发功能中ajax调用的成功,以便您可以测试成功方法中可能具有的任何功能。我以前会使用Sinon.fakeServer,但我没有意识到它会触发原始ajax调用的成功 因此,解决方案是这样做:
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');
});
});
Sinon.server将触发功能中ajax调用的成功,以便您可以测试成功方法中可能具有的任何功能。您的测试似乎没有进行任何ajax调用(您只是在设置间谍)。您确定没有丢失代码吗?您的测试似乎没有进行任何Ajax调用(您只是在设置间谍)。您确定没有丢失代码吗?