I'm writing directive tests for AngularJS with Jasmine, and using templateUrl with them: https://gist.github.com/tanepiper/62bd10125e8408def5cc
However, when I run the test I get the error included in the gist:
Error: Unexpected request: GET views/currency-select.html
From what I've read in the docs I thought I was doing this correctly, but it doesn't seem so - what am I missing here?
Thanks
If this is a unit-test, you won't have access to
$httpBackend.passthrough()
. That's only available in ngMock2E2, for end-to-end testing. I agree with the answers involvingng-html2js
(used to be named html2js) but I would like to expand on them to provide a full solution here.To render your directive, Angular uses
$http.get()
to fetch your template fromtemplateUrl
. Because this is unit-testing andangular-mocks
is loaded,angular-mocks
intercepts the call to$http.get()
and give you theUnexpected request: GET
error. You can try to find ways to by pass this, but it's much simpler to just use angular's$templateCache
to preload your templates. This way,$http.get()
won't even be an issue.That's what the ng-html2js preprocessor do for you. To put it to work, first install it:
Then configure it by adding/updating the following fields in your
karma.conf.js
Finally, in your test code, use the
test-templates
module that you've just created. Just addtest-templates
to the module call that you typically make inbeforeEach
, like this:It should be smooth sailing from here on out. For a more in depth look at this and other directive testing scenarios, check out this post
You could perhaps get the
$templatecache
from the injector and then do something likewhere in place of
<div.....>
you would be putting your template.After that you setup your directive and it should work just fine!
If you're using ngMockE2E or ngMock:
all HTTP requests are processed locally using rules you specify and none are passed to the server. Since templates are requested via HTTP, they too are processed locally. Since you did not specify anything to do when your app tries to connect to
views/currency-select.html
, it tells you it doesn't know how to handle it. You can easily tell ngMockE2E to pass along your template request:Remember that you can also use regular expressions in your routing paths to pass through all templates if you'd like.
The docs discuss this in more detail: http://docs.angularjs.org/api/ngMockE2E.$httpBackend
Otherwise use this:
You'll need to use the
$injector
to access the new backend. From the linked docs:As requested, converting a comment to an answer.
For the people who want to make use of @Lior's answer in Yeoman apps:
Sometimes the way the templates are referenced in karma config and consequently - the names of modules produced by
ng-html2js
don't match the values specified astemplateUrl
s in directive definitions.You will need adjusting generated module names to match
templateUrl
s.These might be helpful:
If this is still not working , use fiddler to see the content of the js file dynamically generated by htmltojs processor and check the path of template file.
It should be something like this
In my case , it was not same as I had in my actual directive which was causing the issue.
Having the templateURL exactly same in all places got me through.
this is example how to test directive that use partial as a templateUrl