我想在我的Jasmine测试中模拟测试数据.这是两个版本:
// version 1:
spyOn(mBankAccountResource, 'getBankAccountData').and.callFake(fakedFunction);
// version 2:
spyOn(mBankAccountResource, 'getBankAccountData').andCallFake(fakedFunction);
Run Code Online (Sandbox Code Playgroud)
当我使用浏览器(Chrome,Firefox)执行测试时,第一个版本可以正常运行.但是,当我使用phantomjs运行相同的测试时,我必须使用第二个版本.否则,它会抱怨函数未定义.
以下是错误消息:
// phantomjs (with version 1)
TypeError: 'undefined' is not an object (evaluating 'spyOn(mBankAccountResource, 'getBankAccountData').and.callFake')
at /home/phil/workspaces/world/basket.angular.ui/basket.angular.ui/test/bankaccount/BankAccountCtrlTest.js:65
at invoke (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular/angular.js:3707)
at workFn (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular-mocks/angular-mocks.js:2149)
undefined
// Chrome (with version 2)
TypeError: Object function () {
callTracker.track({
object: this,
args: Array.prototype.slice.apply(arguments)
});
return spyStrategy.exec.apply(this, arguments);
} has no method 'andCallFake'
at Object.<anonymous> (/home/phil/workspaces/world/basket.angular.ui/basket.angular.ui/test/bankaccount/BankAccountCtrlTest.js:65:59)
at Object.invoke (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular/angular.js:3707:17)
at Object.workFn (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular-mocks/angular-mocks.js:2149:20)
Run Code Online (Sandbox Code Playgroud)
我搜索了Jasmine API,但无法找出哪个版本是正确的.我发现的所有示例似乎都使用第二个版本.
Jasmine的API最近有变化吗?我怎样才能编写我的测试,所以它始终有效?
假设我有
spyOn($cookieStore,'get').and.returnValue('abc');
Run Code Online (Sandbox Code Playgroud)
这对我的用例来说太笼统了.我们随时打电话
$cookieStore.get('someValue') --> returns 'abc'
$cookieStore.get('anotherValue') --> returns 'abc'
Run Code Online (Sandbox Code Playgroud)
我想设置一个spyOn,所以我根据参数得到不同的回报:
$cookieStore.get('someValue') --> returns 'someabc'
$cookieStore.get('anotherValue') --> returns 'anotherabc'
Run Code Online (Sandbox Code Playgroud)
有什么建议?
I am spying a JS method. I want to return different things based on actual argument to the method. I tried callFake and tried to access arguments using arguments[0] but it says arguments[0] is undefined. Here is the code -
spyOn(testService, 'testParam').and.callFake(function() {
var rValue = {};
if(arguments[0].indexOf("foo") !== -1){
return rValue;
}
else{
return {1};
}
})
Run Code Online (Sandbox Code Playgroud)
This is suggested here - Any way to modify Jasmine spies based on arguments?
But it does not work for me.