这里要注意的时,上面的插件大部分都不需要单独安装,因为安装karma的时候已经安装了,这里只有karma-junit-reporter导出插件需要单独安装,想要了解更多的关于配置文件的信息可以,点击这里 karma就讲到这里,想了解更多关于它的信息可以,点击这里 什么是jasmine Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. 上面是jasmine官方文档里对它的解释,下面用中文简单的翻译下 jasmine是一个行为驱动开发的测试框架,不依赖任何js框架以及dom,是一个非常干净以及友好API的测试库. 下面简单的以一个例子来说明它的用法 定义一个测试文件命令为test.js 复制代码 代码如下: describe("A spec (with setup and tear-down)", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
describe("MyApp", function() { // You need to load modules that you want to test, // it loads only the "ng" module by default. beforeEach(module("myApplicationModule"));
// inject() is used to inject arguments of all given functions it("should provide a version", inject(function(mode, version) { expect(version).toEqual("v1.0.1"); expect(mode).toEqual("app"); }));
// The inject and module method can also be used inside of the it or beforeEach it("should override a version and test the new version is injected", function() { // module() takes functions or strings (module aliases) module(function($provide) { $provide.value("version", "overridden"); // override version here }); inject(function(version) { expect(version).toEqual("overridden"); }); }); });
然后我们编写一个测试脚本 复制代码 代码如下: describe("myController function", function() { describe("myController", function() { var $scope; beforeEach(module("myApp")); beforeEach(inject(function($rootScope, $controller) { $scope = $rootScope.$new(); $controller("MyController", {$scope: $scope}); })); it("should create "spices" model with 3 spices", function() { expect($scope.spices.length).toBe(3); }); it("should set the default value of spice", function() { expect($scope.spice).toBe("hello feenan!"); }); }); });
上面利用了$rootScope来创建子作用域,然后把这个参数传进控制器的构建方法$controller里去,最终会执行上面的控制器里的方法,然后我们检查子作用域里的数组数量以及字符串变量是否跟期望的值相等. 想要了解更多关于ng里的控制器的信息,可以点击这里 ng里指令的单元测试 定义一个简单的指令 复制代码 代码如下: var app = angular.module("myApp", []); app.directive("aGreatEye", function () { return { restrict: "E", replace: true, template: "<h1>lidless, wreathed in flame, 1 times</h1>" }; });
然后我们编写一个简单的测试脚本 复制代码 代码如下: describe("Unit testing great quotes", function() { var $compile; var $rootScope; // Load the myApp module, which contains the directive beforeEach(module("myApp")); // Store references to $rootScope and $compile // so they are available to all tests in this describe block beforeEach(inject(function(_$compile_, _$rootScope_){ // The injector unwraps the underscores (_) from around the parameter names when matching $compile = _$compile_; $rootScope = _$rootScope_; })); it("Replaces the element with the appropriate content", function() { // Compile a piece of HTML containing the directive var element = $compile("<a-great-eye></a-great-eye>")($rootScope); // fire all the watches, so the scope expression 1 will be evaluated $rootScope.$digest(); // Check that the compiled element contains the templated content expect(element.html()).toContain("lidless, wreathed in flame, 2 times"); }); });