Jasmine Test in Angular for controller

梦想的初衷 提交于 2020-01-04 02:03:08

问题


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);
    });
});

回答1:


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.



来源:https://stackoverflow.com/questions/20701764/jasmine-test-in-angular-for-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!