ionic2 - Jasmine - how to unit test a TypeScript class that imports another class with static methods -
i have simple typescript class (in ionic application), implements simple "typed" dictionary...
import { utils } './utils'; export class dictionary<t> { constructor(private nocase?: boolean, init?: array<{ key: string; value:t; }>) { .... } }
i have written simple tests it...
import { dictionary } './dictionary'; let dictionary : dictionary<string> = null; describe('dictionary', () => { beforeeach(() => { dictionary = new dictionary<string>(true, []); }); it('should have containskey find value added', () => { dictionary.add("a", "a val"); let exists = dictionary.containskey("a"); expect(exists).tobetruthy() }); });
when run test, following error...
chrome 60.0.3112 (windows 10 0.0.0) error uncaught typeerror: __webpack_imported_module_3__dictionary__.a not constructor @ webpack:///src/shared/utils.ts:19:17 <- test-config/karma-test-shim.js:77758 chrome 60.0.3112 (windows 10 0.0.0) error uncaught typeerror: __webpack_imported_module_3__dictionary__.a not constructor @ webpack:///src/shared/utils.ts:19:17 <- test-config/karma-test-shim.js:77758 chrome 60.0.3112 (windows 10 0.0.0): executed 0 of 0 error (0.422 secs / 0 secs) webpack: compiling...
my problem utils
class have in class being tested (the dictionary
)
this utils
class has bunch of static "utils" methods, (string compares, formatting, etc)
import * moment 'moment'; import 'moment-duration-format'; import * _ 'lodash'; import { translateservice } 'ng2-translate'; import { dictionary } './dictionary'; ..... export class utils { public static guard(obj: any, name: string): void { if (obj == null || obj == undefined) throw (name + " must not null!"); } public static guards(s: string, name: string): void { if (this.isnulloremptyorwhitespace(s)) throw (name + " must not null or empty!"); } ... etc }
the thing use in dictionary
class the utils
static guard
method (that shown above).
is there way can test class includes other class static methods? can mock class's static methods?
while above class trivial, have other stuff test, includes static utils
class.
thanks in advance suggestions.
yes, can add spy mock guard method in utils class.
spyon(utils, 'guard').and.returnvalue(true);
Comments
Post a Comment