得到错误,同时使用与茉莉AngularJS [英] Getting error while using Jasmine with AngularJS

查看:123
本文介绍了得到错误,同时使用与茉莉AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的茉莉有下列code结果来测试我的 AngularJs 项目

I am using jasmin to test my AngularJs project with following code

controllersSpec.js

describe('myApp', function(){

    beforeEach(angular.mock.module('myApp'));

    it('should have a mailctrl', function() {
        expect(myApp.mailctrl).toBeDefined();
    });
});

controller.js 搜索

var myApp = angular.module('myApp', []);

myApp.controller('mailctrl', ['$scope', '$routeParams', '$rootScope', 'getMailData', '$angularCacheFactory',

    function ($scope, $routeParams, $rootScope, getMailData, $angularCacheFactory) {
           ##....controller content....##
    }
]);

SpecRunner.html

<脚本类型=文/ JavaScript的SRC =../的lib /角/ angular.min.js>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =LIB /茉莉-2.0.0 / jasmine.js>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =LIB /茉莉-2.0.0 / jasmine.js>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =LIB /茉莉-2.0.0 /茉莉html.js>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =LIB /茉莉-2.0.0 / boot.js>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =../测试/ lib目录/角/角mocks.js>< / SCRIPT>
  < SCRIPT SRC =../ JS / controller.js>< / SCRIPT>
  < SCRIPT SRC =../测试/单位/ controllersSpec.js>< / SCRIPT>

现在,当我打开 Specrunner.html 浏览器,我收到以下错误搜索结果
预计未定义被定义。结果
我有一个关于上述搜索结果以下问题
1)当我得到了?结果
2)如何使用控制器的$ rootScope变量?结果
3)什么在的 beforeEach的差异(angular.mock.module('对myApp')); 的和的 beforeEach(angular.module('对myApp')); beforeEach(模块('对myApp'));

Now when I am opening Specrunner.html in browser, I am getting following error

Expected undefined to be defined.
I have following question regarding above

1) Where I am getting wrong ?
2) How can I use $rootScope variable of controller ?
3) Whats the difference in beforeEach(angular.mock.module('myApp')); and beforeEach(angular.module('myApp')); and beforeEach(module('myApp'));

编辑:搜索结果
我已经修改了code作为

EDIT :

I have modified the code as

describe('myApp', function(){
    beforeEach(angular.mock.module('myApp'));        
    it('should have a mailctrl', inject(function($rootScope, $controller) {
        var controller = $controller('mailctrl', { $scope: $rootScope.$new(), $rootScope: $rootScope });
        expect(controller).toBeDefined();
}));

和得到下面的错误 - 搜索结果
错误:[$喷油器:unpr]的http://errors.angularjs.org/undefined/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

and getting following error -

Error: [$injector:unpr] http://errors.angularjs.org/undefined/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

如果我在$控制器添加其他PARAMS()(即'$ routeParams','getMailData')错误之际,它们是不确定的。

If I add other params in $controller() (i.e. '$routeParams', 'getMailData') Error comes as they are undefined.

推荐答案

1)你被试图访问你的控制器作为现场模块上犯了一个错误。在您的测试,你应该注射 $控制器 的到您的测试来创建你的控制器。这也就意味着,你应该写你的测试,如以下

1) You are making a mistake by trying to access your controller as a field on a module. in your test you should be injecting $controller into your test to create your controller. What that means is that you should be writing your tests like the following

it('should have a mailctrl', inject(function($rootScope, $controller) {
   var controler = $controller('mainctrl', { $scope: $rootScope.$new(), $rootScope: $rootScope, ...  }) // rest of your dependencies
   expect(controller).toBeDefined();
}));

2)查看1

3) angular.mock.module 用于您的模块注册到喷油器在你的测试的。这是同样的事情模块 angular.module 用于创建,注册或检索您的应用程序模块

3) angular.mock.module is used to register your module to the injector in your tests. It is the same thing as module. angular.module is used to create, register or retrieve a module for your application.

关于你的编辑:

您不提供routeParams和你的控制依赖于这样$喷油器不知道从哪里得到任何其他服务和投掷此错误。 $喷油器有知道为每一位由控制器所需的参数注入。您的控制器取决于

You are not providing routeParams and any other service that your control depends on so $injector does not know where to get from and throwing this error. $injector has to know what to inject for every parameter that is required by your controller. Your controller depends on


  • $ rootScope :由角
  • 提供
  • $范围:可以通过$ rootScope创建新的$()

  • getMailData :这是你的服务,你必须为此提供一个模拟

  • $ routeParams :这是棱角分明的内置结构,但还没有一个专门的模拟,但因为它是一个非常简单的对象,你可以为此提供一个模拟的自己

  • $ angularCacheFactory :这是一个第三方服务,你必须提供这样的模拟,以及

  • $rootScope: provided by angular
  • $scope: can be created by $rootScope.$new()
  • getMailData: This is your service, you have to provide a mock for this
  • $routeParams: This is angular built-in structure but there is not a dedicated mock, but since it is a very simple object you can provide a mock for this yourself.
  • $angularCacheFactory: this is a third party service and you have to provide a mock for this as well

那么,到底你应该创建您的控制器像这样

So in the end you should be creating your controller like this

it('should have a mailctrl', inject(function($rootScope, $controller) {
   var controler = $controller('mainctrl', { 
         $scope: $rootScope.$new(), 
         '$routeParams': {}, // whatever  it needs to be to be able to test your controller
         $rootScope: $rootScope, 
         'getMailData': getMailDataMOCK // this object is created by you in your test code
         '$angularCacheFactory': $angularCacheFactoryMOCK
       }) 
   expect(controller).toBeDefined();
}));

如果你不提供任何的这些参数,然后它会尝试从$喷油器得到它,就会抛出一个异常时未找到。

If you don't provide any of these arguments then it will try to get it from $injector and will throw an exception when not found.

这篇关于得到错误,同时使用与茉莉AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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