用于控制器的 Angular 中的 Jasmine 测试 [英] Jasmine Test in Angular for controller

查看:25
本文介绍了用于控制器的 Angular 中的 Jasmine 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是完整的测试:

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

推荐答案

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

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.

这篇关于用于控制器的 Angular 中的 Jasmine 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆