Jasmine 我是否可以将提供的函数中的原始方法调用为";并称之为“假”;一个茉莉花间谍?
我可以在beforeach中将原始方法保存在一个变量中,然后在afterEach中恢复它,但也许我可以使用spy,它将在测试套件之间自动重置Jasmine 我是否可以将提供的函数中的原始方法调用为";并称之为“假”;一个茉莉花间谍?,jasmine,Jasmine,我可以在beforeach中将原始方法保存在一个变量中,然后在afterEach中恢复它,但也许我可以使用spy,它将在测试套件之间自动重置 spyOn(Ext, "create").andCallFake(function(className){ if (className === 'Waf.view.Viewport') // call the original Ext.create method }); 这可能吗?我使用的是Jasmine 1.3我最后做了这样的事
spyOn(Ext, "create").andCallFake(function(className){
if (className === 'Waf.view.Viewport')
// call the original Ext.create method
});
这可能吗?我使用的是Jasmine 1.3我最后做了这样的事情:
var origFunc = Ext.create;
spyOn(Ext, "create").andCallFake(function(className, classConfig){
if (className === 'Waf.view.Viewport') {
return {};
} else {
return origFunc.apply(null, arguments);
}
});
不同的场景应该相互独立地进行测试。尝试将您的测试结构化为类似这样的内容
beforeEach(function () {
spyOn(Ext, 'create');
});
describe('scenario 1', function () {
beforeEach(function () {
Ext.create.andCallThrough();
});
it('should do something', function () {
// do your assertions
});
});
describe('scenario 2', function () {
beforeEach(function () {
Ext.create.andCallFake(function () {
// faked function
});
// or if you're always returning a constant value, use andReturn
// Ext.create.andReturn({});
});
it('should do something', function () {
// do your assertions
});
});
您可以将原始方法绑定到伪方法中:
var obj = {
method: function(name) { return name + '!'; }
}
var methodFake = function(original, name) {
return 'faked ' + original(name);
}.bind(obj, obj.method)
spyOn(obj, 'method').andCallFake(methodFake);
obj.method('hello') // outputs 'faked hello!'
不管它值多少钱,我不认为这是一个很好的实践,但是最近当我测试一些d3代码时,我想到了这个需求。希望能有所帮助。这是对Jasmine 2.3的一个破解。理想情况下,伪回调应该能够访问原始函数的引用,以便根据需要进行调用,而不是像这样到处乱跳
beforeEach(function () {
spyOn(Ext, 'create');
});
describe('scenario 1', function () {
beforeEach(function () {
Ext.create.andCallThrough();
});
it('should do something', function () {
// do your assertions
});
});
describe('scenario 2', function () {
beforeEach(function () {
Ext.create.andCallFake(function () {
// faked function
});
// or if you're always returning a constant value, use andReturn
// Ext.create.andReturn({});
});
it('should do something', function () {
// do your assertions
});
});
考虑到Jasmine 2.3中可以修改存根策略,以下方法似乎也适用:
var createSpy = spyOn(Ext, "create");
createSpy.and.callFake(function(className){
if (className === 'Waf.view.Viewport'){
createSpy.and.callThrough();
Ext.create(className);
}
});
下面是我如何使用Jasmine和角度服务实现它的。我监视的服务正在我的测试服务的构造函数中调用:
// create the TestBed:
TestBed.configureTestingModule({
providers: [MyInjectedService, ServiceConstructorInjectedService]
});
myInjectedService = TestBed.get(MyInjectedService);
serviceConstructorInjectedService = TestBed.get(ServiceConstructorInjectedService);
it('should...', () => {
let returnValue = 'return this';
spyOn(serviceConstructorInjectedService , 'myFunction').and.callFake((param) => {
if (param === 'testValue') {
return returnValue;
} else {
return ServiceConstructorInjectedService.prototype.myFunction(param);
}
});
});
// instantiate the service again so spy is called
myInjectedService = new MyInjectedService(
TestBed.get(ServiceConstructorInjectedService)
);
谢谢,但答案似乎不是针对我的问题。这正是OP在2014年提出的要求,也是我今天需要的,谢谢!