ANGULARJS test based on Karma and Jasmine

Source: Internet
Author: User
Tags server port

1: Engineering directory structure
[Email protected]:karma-t01$ tree-l 3
.
├──client
│├──app
││└──user
│├──bower_components
││├──angular
││├──angular-mocks
││└──angular-resource
│└──bower.json
├──karma.conf.js
└──readme

7 Directories, 3 files

Project Description: Use Bower for JS package management, use Karma and jasmine to complete basic test. Use the packages that the Bower online installation project relies on:
[Email protected]:client$ Bower Install angular
[Email protected]:client$ Bower Install Angular-resource
[Email protected]:client$ Bower Install Angular-mocks
User Directory structure:
[Email protected]:app$ tree-l 1 user/
user/
├──user.js
├──users.json
└──user.test.js

0 directories, 3 files

2:user.js
/**
* Created by Y on 15-11-24.
*/
' Use strict ';

var app = Angular.module (' Application ', [' Ngresource ']);

App.factory (' Userfactory ', function ($resource) {
Return $resource (' Users.json ', {},{
Query:{method: ' GET ', isarray:true}
});
});

App.controller (' Mainctrl ', function ($scope, userfactory) {
$scope. title = ' Hello AngularJS in action! ';
$scope. Users = Userfactory.query ();
});
3:user.test.js
/**
* Created by Y on 15-11-24.
*/
' Use strict ';

Describe (' Mainctrl ', function () {
var scope,httpbackend;

Beforeeach (Angular.mock.module (' Application '));

Beforeeach (Angular.mock.inject (function ($rootScope, $controller, _$httpbackend_) {

Httpbackend = _$httpbackend_;
Httpbackend.when (' GET ', ' Users.json ')
. Respond ([
{
Name: ' Zhang San ', age:25
},
{
Name: ' John Doe ', age:24
},
{
Name: ' Harry ', age:27
}
]);

Scope = $rootScope. $new ();

$controller (' Mainctrl ', {$scope: scope});
}));

Test begin
It (' should has variable title= ' Hello AngularJS in action! ' ', function () {
Expect (Scope.text). ToBe (' Hello AngularJS in action! ');
});

Test begin
It (' Should fetch list of users ', function () {
Httpbackend.flush ();

Expect (scope.users.length). ToBe (3);
Expect (Scope.users[0].name). ToBe (' Zhang San ');
});


});
4:users.json
[
{
Name: ' Zhang San ', age:25
},
{
Name: ' John Doe ', age:24
},
{
Name: ' Harry ', age:27
}
]
5: For Karma file configuration, Karma uses Jasmine as the test framework by default.
Switch to the directory where you want to place the profile, and then enter the following command in the terminal to create the configuration file:
[Email protected]:karma-t01$ Karma Init karma.conf.js
The configuration information is as follows: Main files node configuration
Karma Configuration
Generated on Tue Nov 23:12:58 gmt+0800 (CST)

Module.exports = function (config) {
Config.set ({

Base path that would be used to resolve all patterns (eg. files, exclude)
BasePath: ",


Frameworks to use
Available Frameworks:https://npmjs.org/browse/keyword/karma-adapter
Frameworks: [' Jasmine '],


List of files/patterns to load in the browser
files: [
' Client/bower_components/angular/angular.js ',
' Client/bower_components/angular-mocks/angular-mocks.js ',
' Client/bower_components/angular-resource/angular-resource.js ',
' Client/app/**/*.js '
],



List of files to exclude
Exclude: [
],


Preprocess matching files before serving them to the browser
Available Preprocessors:https://npmjs.org/browse/keyword/karma-preprocessor
Preprocessors: {
},


Test results reporter to use
Possible values: ' dots ', ' progress '
Available Reporters:https://npmjs.org/browse/keyword/karma-reporter
Reporters: [' progress '],


Web server port
port:9876,


enable/disable colors in the output (reporters and logs)
Colors:true,


Level of logging
Possible values:config. log_disable | | Config. Log_error | | Config. Log_warn | | Config. Log_info | | Config. Log_debug
Loglevel:config. Log_info,


Enable/disable watching file and executing tests whenever any file changes
Autowatch:true,


Start these browsers
Available browser Launchers:https://npmjs.org/browse/keyword/karma-launcher
Browsers: [' Chrome '],


Continuous Integration Mode
If true, Karma captures browsers, runs the tests and exits
Singlerun:false
});
};
6: Test in console input command
[Email protected]:karma-t01$ karma Start Karma.conf.js
INFO [Karma]: Karma v0.12.32 server started at http://localhost:9876/
INFO [Launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket co5e5tax7pv9pzqgaaaa with ID 34588683
Chrome 39.0.2171 (Linux): Executed 2 of 2 SUCCESS (0.06 secs/0.048 secs)

Prompt to perform 2 tests, two successful. 7: Reference link http://bower.io/http://jasmine.github.io/http://karma-runner.github.io/0.13/index.html

ANGULARJS test based on Karma and Jasmine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.