samb90
samb90

Reputation: 1073

Mocha -Parameter in mocked data

I'm mocking a function called isLoggedIn():

auth = {
        isLoggedIn: function () {
            return true;
        }
};

and apply is in a beforeEach loop

beforeEach(function () {
    angular.mock.module(function ($provide) {
        $provide.value('Auth', auth);
    });

});

At the moment isLoggedIn() will always return true. This function needs to be able to return false for some particular tests.

Is there a way to pass a variable into the mock from my tests.

E.g. something like

var loggedIn = ;

auth = {
        isLoggedIn: function () {
            return loggedIn;
        }
};

Upvotes: 0

Views: 51

Answers (1)

Erik Man
Erik Man

Reputation: 844

Yes, you can for instance put it inside your test:

var loggedIn;
beforeEach(function () {
    auth = {
        isLoggedIn: function () {
            return loggedIn;
        }
    };

    angular.mock.module(function ($provide) {
        $provide.value('Auth', auth);
    });

});

It is also possible to have it as a separate file, but for this example it seems overkill.

Upvotes: 1

Related Questions