Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$httpBackend.passThrough() does not exist when running unit tests #2512

Closed
ryanzec opened this issue Apr 25, 2013 · 12 comments
Closed

$httpBackend.passThrough() does not exist when running unit tests #2512

ryanzec opened this issue Apr 25, 2013 · 12 comments

Comments

@ryanzec
Copy link

ryanzec commented Apr 25, 2013

When I try to run my unit tests with karma, I try to do:

$httpBackend.when('GET', /\.html$/).passThrough()

so that the templates that angular tries to load and loaded properly however when running the unit test, it tells me the $httpBackend does not have a method of passThrough().

Right now I and loading the templates manually with jQuery and with the that the tests run fine.

I think this might be related to the issue detailed here : #2370

@tadas-subonis
Copy link

Same here. Using Angular v1.1.5. I would add that it looks that after specifying usage of e2e httpBackend it still tries to use ngMock one.

@megalithic
Copy link

Also having the same issues that @tadas-subonis is having, specifically that ngMockE2E version of $httpBackend isn't working.

@btford btford closed this as completed Aug 24, 2013
@btford
Copy link
Contributor

btford commented Aug 24, 2013

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

@samaxes
Copy link
Contributor

samaxes commented Sep 26, 2013

Still not working with Angular 1.0.8.

@petebacondarwin
Copy link
Member

Can you give a running example of this not working? It may be the configuration of the application or karma?

@hjunaid
Copy link

hjunaid commented Oct 24, 2013

Hi Pete, I am experiencing the same issue . Here is my configuration...

// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html

module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

        // testing framework to use (jasmine/mocha/qunit/...)
        frameworks: ['jasmine'],

        // list of files / patterns to load in the browser
        files: [
            'app/bower_components/jquery/jquery.js',
            'app/bower_components/angular/angular.js',
            'app/bower_components/angular-mocks/angular-mocks.js',
            'app/bower_components/angular-route/angular-route.js',
            'app/bower_components/ng-grid/ng-grid-2.0.7.debug.js',
            'app/scripts/*.js',
            'app/scripts/**/*.js',
            'test/unit/**/*.js',
            'app/scripts/**/*.html'
        ],

        // list of files / patterns to exclude
        exclude: [''],

        // web server port
        port: 8080,

        // level of logging
        // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari (only Mac)
        // - PhantomJS
        // - IE (only Windows)
        browsers: ['Chrome'],
        reporters: ['dots'],

        // Continuous Integration mode
        // if true, it capture browsers, run tests and exit
        singleRun: false,
        plugins: [
            'karma-jasmine',
            'karma-html2js-preprocessor',
            'karma-chrome-launcher',
            'karma-firefox-launcher'
        ]
    }
)
;

}
;


And tests are ....


describe('DataServiceSpec::', function () {
'use strict';

var service, $httpBackend;

beforeEach(module('dotApp'));

beforeEach(inject(function ($injector) {
    $httpBackend = $injector.get('$httpBackend');
    service = $injector.get('service');
    $httpBackend.whenGET('/myData').passThrough();
}));

it('dataService should have been initialised', function () {
    service.getMyData();//invoke $http.get('/myData') call
    $httpBackend.flush();
});

});


Error ...

TypeError: Object # has no method 'passThrough'

....

@hjunaid
Copy link

hjunaid commented Oct 24, 2013

I am trying to test the rest end points. That is fire off http calls to real server and then verify the response. I wonder if Karma + Unit tests is the right approach to do so ...

@andreareginato
Copy link

Same problem for me. I'm trying to test a directive where I mock the API request but I want the test to load the template. As passThrough() is not accessible, tests does not work.

@pkozlowski-opensource
Copy link
Member

@andreareginato it is much easier to preload templates into $templateCache for unit tests, as shown in https://github.com/vojtajina/ng-directive-testing - this way you don't need to fiddle with $http as all templates are already preloaded and it is not necessary to fetch them over XHR so no $http calls are happening.

@andreareginato
Copy link

I finally made it work. Thanks @pkozlowski-opensource. For future rederences here some tips.

karma.conf.js

// template to load in the browser
files: [ 'app/views/templates/default.html', ... ]
// Preprocessor for converting HTML files to AngularJS templates
preprocessors: { 'app/views/templates/default.html': 'html2js' },
// remove the app/ prefix to match the module name with the directive template url
// see next code block
ngHtml2JsPreprocessor: { stripPrefix: 'app/' },

directive.js

// directive template (note it does not set /app/)
var template = 'views/templates/default.html';

The thing that was not clear at first was how template modules are named and that the name must match with the URL of the directive. Now it works. Thanks.

@pkozlowski-opensource
Copy link
Member

Going to close this one as a duplicate of #1434 where someone else is trying to use both unit and e2e mocks together.

@emascarinas
Copy link

i don't think you can call passThrough() on unit test. I think it needs .run method and its only on e2e modules "ngMockE2E".

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

No branches or pull requests

10 participants