Skip to content

Instantly share code, notes, and snippets.

@dac09
Created October 7, 2014 23:03
Show Gist options
  • Save dac09/db750d648c8bad694907 to your computer and use it in GitHub Desktop.
Save dac09/db750d648c8bad694907 to your computer and use it in GitHub Desktop.
Constants in Jasmine unit tests
describe('Test', function() {
beforeEach(function() {
module('App', function($provide) {
$provide.constant('CSRF_TOKEN', 'MOCK_CONSTANT'); // <= mock your constant
});
});
// Tests go here
});
@dac09
Copy link
Author

dac09 commented Oct 7, 2014

Great for $httpBackend tests, as it lets you test if $http requestst use env / constants / config

@georgeblake
Copy link

How do you use that constant value in your specs?

@kart1618
Copy link

This helped. Thanks dac09.

georgeblake,
In my case, I was writing a test for a custom filter, which made use of a constant.
After specifying my constant, I injected it, for my filter to use. Like so:

    beforeEach(function() {
        module("moduleName", function($provide) {
            $provide.constant("mockConstantName", mockConstantValue);
        });
    });

    var filter;

    beforeEach(inject(function (mockConstantName, _$filter_) {
        filter = _$filter_("filterName");
    }));


    it("should return 'result' ", function () {
        expect(filter("input")).toEqual("result");
    });

Hope this helps.

@fmquaglia
Copy link

@dac09 you saved my day dude 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment