Angular中的Jasmine测试用于控制器

时间:2013-12-20 10:42:34

标签: angularjs jasmine angularjs-controller

我收到以下错误:TypeError: undefined is not a function 问题是常见的是模块和工厂,问题出在我的行上

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

这是完整的测试:

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 个答案:

答案 0 :(得分:3)

这不是问题,问题是你不能像在代码中那样将东西注入到函数中。要注入,您需要像inject中那样拨打beforeEach。所以,如果你想注入那个工厂,你需要这个:

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

你有注射方式。这应该有用。