如何测试一个角度应用程序? [英] How to test an Angular app?
问题描述
对于简单的应用程序,如只是一个Hello World我在哪里写测试。
我创建了一个plnkr。
http://plnkr.co/edit/M0GAIE837G3s1vNwTyK8?p=info
现在,这是一个非常简单的plnkr,它不只是显示的Hello World。
现在,如果我想要写为MainCtrl此应用程序即测试..我在哪里插入它?
要运行具有角噶茉莉花测试:
-
您需要从节点打包模块安装业力,从命令窗口中执行:在
NPM安装-g人缘
-
如果您打算与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:
You need to install nodejs, karma runs on top of node
You need to install karma from Node Packaged Modules from your command window execute:
npm install -g karma
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屋!