javascript - Testing angularjs' factory with Karma -
i've written mill angularjs have method returns http promise.
'use strict' angular.module('projdefinitapp') .factory 'testservice', ($http) -> { gettest: (testid) -> $http.get "temp/test.json" } i'm having issues when making test, using karma , jasmine. promise didn't homecoming result. tried create var, phone call function , alter value of var. not updates value:
'use strict' describe 'service: testservice', -> # load service's module beforeeach module 'projdefinitapp' # instantiate service testservice = {} beforeeach inject (_testservice_) -> testservice = _testservice_ 'should something', -> retrieved = undefined testservice.gettest().success ($data,retrieved) -> retrieved = $data homecoming expect(retrieved).tobe(null) homecoming homecoming the console exit looks this
phantomjs 1.9.8 (linux) service: testservice should failed
expected undefined null.
in tests, angular not perform requests made $http service. reason beingness requests queued run in $digest phase of angular lifecycle. need run through angular’s digest cycle manually in tests calling $rootscope.$apply() or $httpbackend.flush() create phone call internally.
one issue see in current test tests aren’t made work asynchronously, there no guarantee retrieved set before expectation called since promises resolved asynchronously. rewrite tests create work asynchronously:
it 'should something', (done) -> testservice.gettest().success ($data) -> expect($data).tobe(null) done() homecoming $rootscope.$apply() homecoming return javascript angularjs karma-runner karma-jasmine
No comments:
Post a Comment