Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js + Karma + Jasmine: Unknown provider for service

I'm writing a test for the following Angular.js service:

var module = angular.module('wp', [ 'aws', 'lodash', 'jquery', 'moment', 'wp.model' ]);

/**
 * Wordpress service.
 */
module.service('wpService', function(_, $http, $q, $aws, Post) {
    var self = this;

    /**
     * HTTP request.
     */
    this.http = function(config) {
        var $config = _.clone(config);

        if ($config.user && $config.password) {
            $config.headers = $config.headers || {};
            $config.headers.Authorization = 'Basic ' + btoa($config.user + ':' + $config.password);
        }

        return $http($config);
    };

    // ..
}

The test case is as follows:

/**
 * Unit tests for wpService.
 */
describe('apService', function() {

    var wpService;

    beforeEach(angular.module('wp'));

    beforeEach(inject(function(_wpService_) {
        wpService = _wpService_;
    }));

    it('is defined', function() {
        expect(wpService).toBeDefined();
    });
});

This looks to be about as text-book as it gets. Unfortunately, I'm getting the following error:

Chrome 43.0.2357 (Mac OS X 10.10.3) apService is defined FAILED
TypeError: queueableFn.fn.call is not a function
Error: [$injector:unpr] Unknown provider: wpServiceProvider <- wpService
http://errors.angularjs.org/1.4.1/$injector/unpr?p0=wpServiceProvider%20%3C-%20wpService
    at /Users/jdolan/Coding/tuuli-syndicate/bower_components/angular/angular.js:68:12

My karma.config.js includes the modules as well as angular-mocks.js:

// list of files / patterns to load in the browser
    files : [ 'bower_components/jquery/dist/jquery.js',
              'bower_components/lodash/lodash.js',
              'bower_components/moment/moment.js',
              'bower_components/x2js/xml2json.js',
              'bower_components/aws-sdk/dist/aws-sdk.js',
              'bower_components/angular/angular.js',
              'bower_components/angular-route/angular-route.js',
              'bower_components/angular-mocks/angular-mocks.js',
              'app/**/*.js',
              'tests/**/*.js' ],

I'm using Angular 1.4.1, Karma 0.12.36.

like image 672
jdolan Avatar asked Jun 23 '15 23:06

jdolan


People also ask

How to write angular tests with Jasmine and Karma?

Here are the main Jasmine methods: Writing tests with Jasmine and Karma is very easy, so, we will create a basic Angular application, then create a simple Angular component and service. Then, we will write some test cases for Angular component, and also write unit test a service with HttpTestingController.

What is karma in angular 12?

Karma in Angular 12 Karma is a test runner tool, it creates a browser instance, run tests to provide the expected results. The benefit of using Karma is that it can be operated via command line and It refreshes the browser automatically whenever we make even minor changes in our app.

What are the dependencies of Jasmine?

1 jasmine-core. Jasmine is the framework we are going to use to create our tests. It has a bunch of functionalities to allow us the write different kinds of tests. 2 karma. Karma is a task runner for our tests. ... 3 The rest of the dependencies are mainly reporters for our tests, tools to use karma and jasmine and browser launchers.

What is the default browser for Karma testing?

By default it is chrome but you can install and use other browser launchers. The angular-cli configuration of karma uses the file “test.ts” as the entry point of the tests for the application. Let’s take a look to that file; We have a lot of things going on here.


1 Answers

Read the angular-mocks example here closely.

The angular.module() function returns an actual angular module, while module() is short for angular.mock.module(). Replace this line in your code and you should be all set:

beforeEach(module('wp'));
like image 154
michaelgmcd Avatar answered Oct 12 '22 12:10

michaelgmcd