Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine Test in Angular for controller

I get the following error: TypeError: undefined is not a function The problem is that the common is module and a factory and the problem is on my line

var ctrl = $controllerConstructor("resetPasswordSentScreen", { $scope: scope, common: common}); 

Here is the full test:

describe('resetPasswordSentScreen', function () {

    var scope, $controllerConstructor;


    beforeEach(module('common', 'app'));

    beforeEach(inject(function($controller, $rootScope) {
        scope = $rootScope.$new();
        $controllerConstructor = $controller;
    }));

    it('it should navigate to the correct url when backToLogin is called ', function (common) {
        var ctrl = $controllerConstructor("resetPasswordSentScreen", { $scope: scope, common: common });
        var mocklocation = sinon.stub({ url: function () {}});
        expect(scope.backToLogin()).toBe(mocklocation.url);
    });
});
like image 994
Maria Christou Avatar asked Apr 26 '26 12:04

Maria Christou


1 Answers

That is not the problem, the problem is that you can't inject stuff into your functions like you do in your code. To inject you need to call inject as you did in the beforeEach. So, if you want to inject that factory, you need this:

it("message", inject(function(common) {
 ...
}));

There is how you inject. That should work.

like image 90
Jesus Rodriguez Avatar answered Apr 29 '26 11:04

Jesus Rodriguez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!