Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

"Unexpected request: GET .../.html" on Karma tests #198

Closed
igorauad opened this issue Oct 6, 2014 · 9 comments
Closed

"Unexpected request: GET .../.html" on Karma tests #198

igorauad opened this issue Oct 6, 2014 · 9 comments

Comments

@igorauad
Copy link
Contributor

igorauad commented Oct 6, 2014

When trying to integrate tests into my project, an error in which the views (.html files) are requested happens when I call $httpBackend.flush() in each test. Something likes this:

Error: Unexpected request: GET public/modules/landing/views/landing-consumers.client.view.html
    No more request expected

This issue has been discussed within the UI-router group (see angular-ui/ui-router#212). Did any of you had this and managed to solve?

@simon-at-angelsdice
Copy link

I was having the same issue. I have discovered by removing Angulartics from my site that my tests now pass.

@igorauad
Copy link
Contributor Author

Hi @simon-at-angelsdice , thank you for the reply. I did not remove Angulartics. Instead, I solved this problem by doing exactly as described in http://stackoverflow.com/a/23670198/2859410

Now, I still have one pending issue (again due to ui-router). It is worth to share here, because eventually someone else has the same issue. This one is an Unexpected request due to the fact that I'm using an abstract state in the $stateProvider and one of its child states purposely has a blank url (namely url: ''). This for some reason causes an unexpected request. I described the details of the issue in angular-ui/ui-router#1430

@Potstickers
Copy link

I ran into this problem a while ago. I did have an abstract state like you as well. This karma preprocessor solved the problem for me: karma-ng-html2js-preprocessor.

@igorauad
Copy link
Contributor Author

Thank you very much, @Potstickers . In this case, I would have to manually load the HTML views as modules into the test routine, correct?

@Potstickers
Copy link

yes. If you're still on the pre-0.4 branch, once you've installed the dependency

  • in config/env/all.js: add something like html:['path/to/ajax-loaded/templates'] to the assets obj. e.g: 'public/modules/**/*.html'
  • then in karma.conf.js, modify:
   //modify files option to cat the html files
   files: applicationConfiguration.assets.lib.js.concat(applicationConfiguration.assets.js, applicationConfiguration.assets.tests,applicationConfiguration.assets.html),
   //and add these 2 options
   preprocessors: {
     'public/modules/**/*.html': ['ng-html2js']
   },
   ngHtml2JsPreprocessor: {
     stripPrefix: 'public/',
     moduleName: 'ngTemplates' //you can name this whatever you want
   }
  • finally to use, just call the module with beforeEach in your ng tests: beforeEach(angular.module('ngTemplates'))

Hope that helps.

@igorauad
Copy link
Contributor Author

Thank you so much @Potstickers , it really helped. The warning still shows up, though. Maybe I made a mistake somewhere. Did the abstract state you mentioned you had also have a blank url (url:'')?

@Potstickers
Copy link

No actually. My use was like this:

$stateProvider.
state('home', {
  abstract: true,
  template: '<div data-ui-view=""></div>'
}).
state('home.loggedOut', {
  url: '/',
  templateUrl: 'home.logged-in.html'
}).
state('home.loggedIn', {
  url: '/',
  templateUrl: 'home.logged-out.html'
});

Here's the SO answer that pointed me in this direction: http://stackoverflow.com/questions/15214760/unit-testing-angularjs-directive-with-templateurl
and the SO answer that inspired the above states:
http://stackoverflow.com/questions/25191768/angular-ui-router-nested-states-for-home-to-differentiate-logged-in-and-logged

@SrinivasNarayansetty
Copy link

Dear Potstickers ,
I Have tried same ode whatever u have mentioned in Karma Configuration file but i am not using any routing in my project .It is Showing an error Like this.Can You Help me in this

screenshot from 2016-05-19 11-25-51

@Atul-sac
Copy link

Atul-sac commented Dec 3, 2017

Hi,
I am using $routeProvider and i have started facing same issue from 10 days back. same test cases were working before approx 10 days, but when i did fresh installation of all dependencies mentioned in package.json. my all test cases started failing which are dependent on $http service call and also which are based on $watch (basically where $apply() is called manually).
I am getting basically these two types of error -

  1. Error: Unexpected request: GET templates/home.html No more request expected
  2. Error: Unexpected request: GET API_URL No more request expected

It seems that few wrong commits has been made recently on KARMA/JASMINE, which are causing these issues, as mine old code base is working fine, only after installing fresh dependencies these test cases are failing now.

I am using below dependencies -

"devDependencies": {
"angular": "^1.6.5",
"angular-mocks": "^1.6.5",
"angular-route": "^1.6.5",
"browserify-istanbul": "^2.0.0",
"grunt": "^1.0.1",
"grunt-browserify": "^5.0.0",
"grunt-contrib-connect": "^1.0.2",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jshint": "^1.1.0",
"grunt-karma": "^2.0.0",
"istanbul": "^0.4.5",
"jasmine-core": "^2.6.4",
"karma": "^1.7.0",
"karma-bro": "^0.11.1",
"karma-browserify": "^5.1.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.4"
}

Please help me out.
Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants