How to mock services in tests with instance-initializers


#1

Hello

We were used to mock services in tests as follows:

test('Authentication service should work', function () {

        expect(1);

        var authManager = App.__container__.lookup('service:authentication.auth-manager');

        // mock the transition
        authManager.transitionTo = function (r) {
            equal(r, 'credentials.signin');
        };

        return authManager
            .initializeAuthentication()
            .finally(disconnect);

});

But now with instance initializer to be executed when visit helper is called I do not know where to mock services. At example above we do not use visit helper at all. By the way visit('/') andThen will not work us, because at least there are many initializations and redirects in application route, which I do not want to be executed.

Do anybody have some advice.

Regards Jan


#2

Let me me rephrase the question.

How to mock services with ember > 1.13 with instance intializer before any visits happen. I was able to run instance initializer myself, but then the visit helper fails because instance initializers has been already executed.

test('Authentication service should work', function () {

        expect(1);
       
       // my helper to run instance initializer in tests without frontend Generally services tests       
       runInstanceInitializers(App)
       
       // way to retrieve services from container regarding the docs of 1.10 
       // http://guides.emberjs.com/v1.10.0/understanding-ember/debugging/
       // how to do it in 1.13 ?
       var authManager = App.__container__.lookup('service:authentication.auth-manager');

        // mock the transition
        authManager.transitionTo = function (r) {
            equal(r, 'credentials.signin');
        };

        // do the visit
        visit('/auth');
});