如何测试一个角度应用程序? [英] How to test an Angular app?

查看:131
本文介绍了如何测试一个角度应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于简单的应用程序,如只是一个Hello World我在哪里写测试。

我创建了一个plnkr。
http://plnkr.co/edit/M0GAIE837G3s1vNwTyK8?p=info

现在,这是一个非常简单的plnkr,它不只是显示的Hello World。
现在,如果我想要写为MainCtrl此应用程序即测试..我在哪里插入它?


解决方案

要运行具有角噶茉莉花测试:


  1. 您需要安装 的NodeJS,因果报应节点之上运行


  2. 您需要从节点打包模块安装业力,从命令窗口中执行:在

     NPM安装-g人缘


  3. 如果您打算与Chrome和Firefox上运行这一点,你在Windows上运行这一点,你需要添加2的环境变量的:



   CHROME_BIN = [克罗默安装路径/的chrome.exe]
   FIREFOX_BIN = [Firefox安装路径/ firefox.exe]

    4.回到你的项目文件夹中使用命令窗口一旦有可以执行:

因果报应的init 

只需点击进入,直到它完成;底线这将创建一个文件名为:

 karma.config.js 

在我的项目这个文件看起来像这样,你可能会包括不同的设置了一些有用的意见:


    module.exports =功能(配置){
      config.set({
        基本路径:'',
        框架:['茉莉'],
        文件:
          ../app/*.js',
          ../app/lib/angular.js',
          ../app/lib/angular-route.min.js',
          ../app/lib/angular-mocks.js',
          ../app/app.js',
          控制器/ *。JS',
          服务/ *。JS',
        ]
        排除:
        ]
        记者:进步]
        端口:9876,
        颜色:真,
        LOGLEVEL:config.LOG_INFO,
        autoWatch:真实,
        浏览器:['铬','火狐'],
        captureTimeout:60000,
        singleRun:假的
      });
    };

重要:请确保您已经在配置角嘲笑,在函数是模块

    5.回到你的命令行窗口,浏览您的 karma.config.js 文件和并执行:

人缘启动

在这一点上,你会好去开始写茉莉测试。

为控制器的简单茉莉测试将是:

 描述('MainCtrl',函数(){
    变量$范围,$ rootScope,createController;    beforeEach(注入(函数($喷油器){
        $ rootScope = $ injector.get('$ rootScope');
        $范围= $ rootScope $新的()。
    变量$控制器= $ injector.get('$控制器');        createController =功能(){
            返回$控制器('MainCtrl',{
                '$范围:$范围
            });
        };
    }));    它('应该有一个......,函数(){
        VAR控制器= createController();
//做你的测试这里...    });
});

For simple application such as just a "Hello World" where do I write tests.

I have created a plnkr. http://plnkr.co/edit/M0GAIE837G3s1vNwTyK8?p=info

Now this is a very simple plnkr, which does nothing but display Hello World. Now if I want to write a test for this Application i.e for MainCtrl.. where do I plug it in ?

解决方案

To run test with Angular-Karma-Jasmine:

  1. You need to install nodejs, karma runs on top of node

  2. You need to install karma from Node Packaged Modules from your command window execute:

    npm install -g karma

  3. If you plan to run this with Chrome and Firefox and you are running this on windows you need to add 2 environment variables:

   CHROME_BIN = [Crome installation path/chrome.exe]
   FIREFOX_BIN =[Firefox installation path/firefox.exe]

    4. Go back to your project folder using the command window once there you can execute:

karma init

Just hit enter until it finishes; bottom line this will create a file named:

karma.config.js

In my project this file looks like this, yours probably will include some helpful comments on the different settings:

    module.exports = function(config) {
      config.set({    
        basePath: '',    
        frameworks: ['jasmine'],    
        files: [
          '../app/*.js',
          '../app/lib/angular.js',
          '../app/lib/angular-route.min.js',
          '../app/lib/angular-mocks.js',         
          '../app/app.js',
          'controllers/*.js',
          'services/*.js',
        ],    
        exclude: [
        ],    
        reporters: ['progress'],    
        port: 9876,    
        colors: true,    
        logLevel: config.LOG_INFO,    
        autoWatch: true,    
        browsers: ['Chrome','Firefox'],
        captureTimeout: 60000,
        singleRun: false
      });
    };

Important: make sure you included angular-mocks in your configuration, the inject function is on that module.

    5. Go back to your command window, navigate where your karma.config.js file is and and execute:

karma start

At this point you will be good to go to start writing tests with jasmine.

a simple jasmine test for your controller will be:

describe('MainCtrl', function() {
    var $scope, $rootScope, createController;

    beforeEach(inject(function($injector) {
        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();


    var $controller = $injector.get('$controller');

        createController = function() {
            return $controller('MainCtrl', {
                '$scope': $scope
            });
        };
    }));

    it('should have a...', function() {
        var controller = createController();
// do your testing here...

    });
});

这篇关于如何测试一个角度应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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