如何利用摩卡在Angular.js测试与angular.module创建控制器()控制器() [英] how to test controllers created with angular.module().controller() in Angular.js using Mocha

查看:182
本文介绍了如何利用摩卡在Angular.js测试与angular.module创建控制器()控制器()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有angular.module创建控制器()。控制器()像在这种情况下

I have a controller created with angular.module().controller() like in this situation

myModule = angular.module('myApp.controllers', [])
                   .controller('testCtrl', ['$scope', function($scope){
                           $scope.test = 'this is a test';
                    }]);

现在,我需要使用摩卡来测试我的控制器正常工作。在角有当控制器声明为全局函数(例如一些例子。 http://docs.angularjs.org/tutorial/step_04 ),所以他们用

now, I need to use mocha to test if my controller is working properly. In Angular there are some examples when controllers are declared as global functions ( ex. http://docs.angularjs.org/tutorial/step_04 ), so they use

function PhoneListCtrl() {...}
.....
beforeEach(function() {
   scope = {},
   ctrl = new PhoneListCtrl(scope);
});

it('shod test whatever PhoneListCtrl does ', function() {
   expect(scope.someProp).toBe('whateverValue');
});    

所以问题是:

1),我该怎么做类似的测试正在使用angular.module宣布控制器()控制器()

1) how can I do a similar test for controllers that are declared using angular.module().controller()

2)如何使用摩卡做

推荐答案

AngularJS提供模拟,从而使提供一些有用的功能依赖注入,同时测试。

AngularJS provides mocks that make available some useful functions for dependency injection while testing.

  • module
  • inject

(茉莉)

比方说,我们要执行从href=\"https://docs.angularjs.org/tutorial/step_02\" rel=\"nofollow\">官方教程的

Let's say we want to perform the first test from the official tutorial and we have defined a controllers module. (you could namespace the module name, but I want to keep it simple)

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

Controllers.controller('PhoneListCtrl', ['$scope', function($scope){
    $scope.phones = [{name: "Nexus S", snippet: "Fast..."},
                     {name: "Motorola XOOM...", snippet: "The Next...."},
                     {name: "MOTOROLA XOOM...", snippet: "The Next, Next..."}];
}]);

然后,我们创建了一个应用模块,并注入它我们的控制器模块

Then we create a module for out app and inject it our controllers module

var PhonesApp = angular.module('phoneApp', ['controllers']);

最后,我们可以测试它像这样

Finally we can test it like this

describe('phonesApp', function() {
    describe('phoneApp controllers', function() {
        beforeEach(module('controllers'));
        describe('PhoneListCtrl', function() {
            it('should create "phones" model with 3 phones',
                inject(function($rootScope, $controller) {

                var scope = $rootScope.$new();
                var ctrl = $controller("PhoneListCtrl", {$scope: scope });
                expect(scope.phones.length).toBe(3);
            }));
        });
    });
});

我没有做它的摩卡咖啡,但我想这个过程是相似的。

I haven't done it in mocha, but I guess the process is similar.

PD:我没有使用CoffeeScript的教程,这里有相关位 https://gist.github.com/ 4163147

Pd: I did the tutorial using CoffeeScript, here are the relevant bits https://gist.github.com/4163147

这篇关于如何利用摩卡在Angular.js测试与angular.module创建控制器()控制器()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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