如何在角度js文件中编写变量的单元测试。javascript单元测试变量和代码没有封装在函数内
fooFactory.spec.js
..
describe('test fooFactory', function(){
it('test if statement', function(){
expect(?).toBe(?);
// how to write a test to pass values to testVar
// testVar runs before I can assign value to it.
// even if I have setters and getters how can I retest the if statement
});
});
..
fooFactory.js
(function() {
angular.module('MyApp').factory('fooFactory', fooFactory);
function fooFactory(someOtherFile){
var testVar = someOtherFile.someOtherfunc;
if(testVar){
// want to test this code. has 10 line of code
}
...
function foo(){
//does something and I can test this
}
...
return {
foo:foo
}
}
})();
我怎么之前赋值的testvar如果语句运行
if(testVar){
// how do I test this code?
}
我应该封装整个如果在一个功能,并使之通过的回报。
bar();
function bar(data){
if(data){
testVar = data;
}
if(testVar){
// how do I test this code?
}
}
return {
foo: foo,
bar: bar
}
有没有更好的方法来做到这一点。 或者js文件应该首先有setter和getters。谢谢
这取决于你在里面有什么'if(testVar){' – jcubic
@jcubic sry for confusion,我实际上希望能够传递一个值给testVar,以便可以测试里面的代码。 – patz