【发布时间】:2016-09-09 14:44:27
【问题描述】:
如何为 Angular js 文件中的变量编写单元测试。
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
}
}
})();
如何在 if 语句运行之前为 testVar 赋值
if(testVar ){
// how do I test this code?
}
我是否应该将整个 if 封装在一个函数中并通过 return 传递。
bar();
function bar(data){
if(data){
testVar = data;
}
if(testVar ){
// how do I test this code?
}
}
return {
foo: foo,
bar: bar
}
有没有更好的方法来做到这一点。 或者js文件首先应该有setter和getter。谢谢
【问题讨论】:
-
这取决于你在
if (testVar) {里面有什么 -
@jcubic sry 混淆,我实际上希望能够将一个值传递给 testVar 以便可以测试里面的代码。
标签: javascript angularjs unit-testing jasmine karma-jasmine