How do we clear spy programmatically in Jasmine?(我们如何在 Jasmine 中以编程方式清除间谍?)
问题描述
我们如何以编程方式清除 jasmine 测试套件中的间谍?谢谢.
How do we clear the spy in a jasmine test suite programmatically? Thanks.
beforeEach(function() {
spyOn($, "ajax").andCallFake(function(params){
})
})
it("should do something", function() {
//I want to override the spy on ajax here and do it a little differently
})
推荐答案
我不确定这是否是个好主意,但您可以简单地将函数上的 isSpy
标志设置为 false:
I'm not sure if its a good idea but you can simply set the isSpy
flag on the function to false:
describe('test', function() {
var a = {b: function() {
}};
beforeEach(function() {
spyOn(a, 'b').andCallFake(function(params) {
return 'spy1';
})
})
it('should return spy1', function() {
expect(a.b()).toEqual('spy1');
})
it('should return spy2', function() {
a.b.isSpy = false;
spyOn(a, 'b').andCallFake(function(params) {
return 'spy2';
})
expect(a.b()).toEqual('spy2');
})
})
但是,如果您需要间谍的其他行为,则为这种情况创建一个新套件可能是一个更好的主意.
But maybe its a better idea to create a new suite for this case where you need an other behavior from your spy.
这篇关于我们如何在 Jasmine 中以编程方式清除间谍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们如何在 Jasmine 中以编程方式清除间谍?


- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01