javascript - AngularJS + Jasmine Unit Test: How to make an angular.constant visible inside the angular.forEach()? -
how make angular.constant visible inside angular.foreach()?
in below sample, angular.constant accessible in parent , child describe blocks. trying optimize code, using hash , looping through run tests, started using angularjs.foreach(). but, appears that, angular.constant not visible inside angular.foreach(). idea why constant not accessible (while service is) inside angular.foreach()?
describe("utilsservice:", function(){ beforeeach(module('utilsservice')); var kib; var gib; beforeeach(inject(function(_kib_){ kib = _kib_; })); beforeeach(inject(function(_gib_){ gib = _gib_; })); var utilsservice; beforeeach(inject(function(_utilsservice_){ utilsservice = _utilsservice_; })); describe('constant values', function() { /* below test works fine */ it("kib constant in describe", function(){ console.log("kib value " + kib); expect(kib).toequal(1024); }); tests = { 'kib value': { 'inputs': { 'constant': kib }, 'expected': { 'value': 1024 } }, 'gib value': { 'inputs': { 'constant': gib }, 'expected': { 'value': 1024 * 1024 } } } angular.foreach(tests, function(t_hash, t_name){ var val = t_hash.inputs.constant; var exp_val = t_hash.expected.value; /* below test failing */ it(t_name, function(){ console.log("tc: " + test_api_name + " " + t_name ); expect(val).toequal(exp_val); }) }); });
note: call service functions (like below comparesize()) working fine.
describe('comparesize()', function() { test_api_name = 'comparesize()'; tests = { 'size1 greater size2': { 'inputs': { 'size1_unit': '2 tb', 'size2_unit': '2048 kb' }, 'expected': { 'return': 1 } }, 'float size1 greater int size2': { 'inputs': { 'size1_unit': '1.50 tb', 'size2_unit': '1 tb' }, 'expected': { 'return': 1 } } } angular.foreach(tests, function(t_hash, t_name){ var size1_unit = t_hash.inputs.size1_unit; var size2_unit = t_hash.inputs.size2_unit; var exp_ret = t_hash.expected.return; it(t_name, function(){ console.log("tc: " + test_api_name + " " + t_name ); var ret = utilsservice.comparesize(size1_unit, size2_unit); expect(ret).toequal(exp_ret); }) });
i think problem (problem 1) beforeeach() called before each it(), when define "tests" "out" of it() , kib/gib undefined.
(you can verify order of calls using console.log)
can solve putting instantiation of tests inside beforeeach()
and putting foreach loop it().
example:
var kib; var gib; var tests; beforeeach(function(inject(_kib_, _gib_) ){ console.log("set kib , gib"); kib = _kib_; gib = _gib_; console.log("define tests"); tests = tests || { 'kib value': { 'inputs': { 'constant': kib }, 'expected': { 'value': 1024 } }, 'gib value': { 'inputs': { 'constant': gib }, 'expected': { 'value': 1024 * 1024 } } } });
but want suggest different way test constants:
describe("constant values:", function(){ it("kib value should 1024", function(){ expect(kib).toequal(1024); }); it("gib value should 1024*1024", function(){ expect(gib).toequal(1024*1024); }); });
.. or more punctual:
describe("constant: kib", function(){ var kib; beforeeach(inject(function(_kib_){ kib = _kib_; })); it("its value should 1024", function(){ expect(kib).toequal(1024); }); }); describe("constant: gib", function(){ var gib; beforeeach(inject(function(_gib_){ gib = _gib_; })); it("its value should 1024*1024", function(){ expect(gib).toequal(1024*1024); }); });
honestly, think these right tests do, but, in case if/when "problem 1" (definition of kib/gib out of it()) solved way job:
describe("constants defined", function(){ [ { name: "kib value should 1024", value: kib, expectedvalue: 1024 }, { name: "gib value should 1024*1024", value: gib, expectedvalue: 1024*1024 } ] .foreach(function(test){ it(test.name, function(){ expect(test.value).toequal(test.expectedvalue); }) }); });
alessandro
Comments
Post a Comment