角度jsファイルの変数の単体テストを書く方法。文が関数内にカプセル化されていない変数とコードをテストするjavascriptユニット
if(testVar){
// how do I test this code?
}
を実行する場合、私は関数内であれば全体をカプセル化し、リターンを通してそれを渡す必要があります前に、私はtestVarに値を割り当てない方法
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
}
}
})();
。
bar();
function bar(data){
if(data){
testVar = data;
}
if(testVar){
// how do I test this code?
}
}
return {
foo: foo,
bar: bar
}
これを行うより良い方法がありますか? jsファイルにセッターとゲッターが最初にあるはずですか?ありがとう
混乱のために 'if(testVar){' – jcubic
@jcubic sryの中にあるものに依存します。実際には、内部のコードをテストできるようにtestVarに値を渡したいと思っています。 – patz