Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection in services #11

Open
camposcristian opened this issue Jan 24, 2014 · 2 comments
Open

Dependency Injection in services #11

camposcristian opened this issue Jan 24, 2014 · 2 comments

Comments

@camposcristian
Copy link

I'm trying to figure out the best way to fill a dependency from a service midway test.

// Service

$provide.factory('$dashboard', ['$http', '$userToken', function ($http, userToken1) {

    var url = window.location.host + '/dashboard/',
        files = {},
        userTok = userToken1;

}]);

// Unit test

describe('UnitTest: dashboardService', function () {

var _userToken = 'aUserToken';
var $dashboard;
var $httpBackend;

// load the service's module
beforeEach(function () {
    module('App', 'mockedResources', function ($provide) {
        $provide.value('$userToken', _userToken);
    });
    inject(function ($injector) {
        $dashboard = $injector.get('$dashboard');
        $httpBackend = $injector.get('$httpBackend');
    });
});

// instantiate service
it('should contain an $dashboardService', function () {
    expect($dashboard).not.toBe(null);
});

});

// Midway test

describe('TestDriver: dashboardService', function () {

var _userToken = 'aUserToken',
$dashboard,
tester;

// load the service's module
beforeEach(function () {
    if (tester) {
        tester.destroy();
    }

    tester = ngMidwayTester('App');

    tester.module('App', function ($provide) {
        $provide.value('$userToken', _userToken);
    });
});

it('should contain an $dashboardService', function () {

    tester.module('App', function ($provide) {
        $provide.value('$userToken', _userToken);
    });

    $dashboard = tester.inject('$dashboard');

    expect($dashboard).not.toBe(null);
});

});

Everytime I run grunt test it fails. The error is

Error: Unknown provider: $userTokenProvider <- $userToken <- $dashboard

Any ideas?

@jimleroyer
Copy link

+1 same problem here

@jimleroyer
Copy link

I've brought a simple modification that introduces a callback function that is called prior to the AngularJS boostrap. Hence I can load any dependencies this way. Might not be the optimal solution but it works, if someone needs the same:

https://www.npmjs.com/package/ng-midway-tester-jlr
https://github.com/jimleroyer/ngMidwayTester

The actual changes:
jimleroyer@ed176f6

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

No branches or pull requests

2 participants