templateUrl testing
Closed this issue · 6 comments
Hi Vojta,
Nice work on Karma, i'm enjoying using it so thanks. Could you give an example of testing directives which use templateUrl please?
I'm using jasmine-jquery. Would you recommend using jasmine.getFixtures or to use.
module(<template-path>)
I've not got either of these solutions working yet.
@spenoir the tab directive from this repo is using templateUrl:
https://github.com/vojtajina/ng-directive-testing/blob/master/js/tabs.js#L26
You can also find more complex examples in this repo prepared by Igor Minar and myself:
https://github.com/IgorMinar/directives-workshop
Unless you've got more specific questions....
Thanks for the links.
I guess I'm talking about loading fixtures in really. When I test a directive that uses a template file I get unexpected GET errors for the template. I can fix this by using jasmine-jquery.getFixtures() and manually adding the response to the templateCache, but I can't see any other more simple way of loading html into a test.
@spenoir I'm a bit confused.... If you put all the templates into $templateCache as pointed out in the above links you shouldn't see any HTTP requests under unit tests. Which makes sense since you definitively don't want to trigger HTTP requests while unit testing as this would make them run slower.
Not sure what you mean by "loading fixtures" exactly, though...
I'm using a templateUrl that is different to the physical disk location. Could that be causing my unexpected get request? I'm using your examples as closely as possible.
Loading fixtures means loading data into tests rather than hard coding, its fairly standard in django envs at least...
@spenoir the whole point of this example is to show how to use ng-html2js
preprocessor. The example does use templateUrl. It sounds like you need to change the preprocessor configuration (cacheIdFromPath
, see https://github.com/karma-runner/karma-ng-html2js-preprocessor).
@vojtajina Even I am getting this message. I am trying to load the template Url by following this example but I am getting unexpected 'GET' errors. Any idea where I could be missing.
In karma conf.js
I am loading the files as
files: [
'../app/libs/angular/angular.js',
'../app/libs/angular-mocks/angular-mocks.js',
'../app/js/controllers/*.js',
'../app/js/directives/*.js',
'karma-spec.js',
'../app/templates/helloworld.html'
],
and configuring as
preprocessors: {
'../app/js/controllers/*.js':['coverage'],
'../app/templates/helloworld.html':'html2js'
},
ngHtml2JsPreprocessor:{
stripPrefix:'../app',
moduleName: "my.templates"
},
And in the test case
loading the module as beforeEach(module('my.templates'));