茉莉花试验角的控制器 [英] Jasmine Test in Angular for controller

查看:129
本文介绍了茉莉花试验角的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:类型错误:未定义不是一个函数
的问题是,常见的是模块和一个工厂,问题是在我的行

  VAR CTRL = $ controllerConstructor(resetPasswordSentScreen,{$适用范围:适用范围,常见的有:普通});

下面是完整的测试:

 描述('resetPasswordSentScreen',函数(){    VAR范围,$ controllerConstructor;
    beforeEach(模块('普通','应用'));    beforeEach(注(函数($控制器,$ rootScope){
        范围= $ rootScope $新的()。
        $ controllerConstructor = $控制器;
    }));    它(它应该导航到正确的网址时,backToLogin被称为,功能(普通){
        VAR CTRL = $ controllerConstructor(resetPasswordSentScreen,{$适用范围:适用范围,常见的有:普通});
        VAR mocklocation = sinon.stub(网址:{url:功能(){}});
        期待(scope.backToLogin())TOBE(mocklocation.url)。
    });
});


解决方案

这不是问题,问题是,像你在code你不能注入东西到你的功能。注入需要调用如您在 beforeEach 一样。所以,如果要注入的工厂,你需要这样的:

 它(信息,注入(功能(普通){
 ...
}));

有是你如何注入。这应该工作。

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

解决方案

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.

这篇关于茉莉花试验角的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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