Jasmine
describe(string,function) //suite测试集,可包含多个Specs(it),每个Specs(it)可包含多个expect
describe("A suite", function() { it("contains spec with an expectation", function() { expect(true).toBe(true); }); });
beforeAll,beforeEach,afterAll,afterEach //beforeAll:每个suite(即describe)中所有spec(即it)运行之前运行, beforeEach:每个spec(即it)运行之前运行
(function(){ describe("Test 'this'", function() { beforeEach(function() { this.testCount = this.testCount || 0; this.testCount++; }); afterEach(function() { //this.testCount = 0; //无论是否有这行,结果是一样的,因为this指定的变量只能在每个spec的beforeEach/it/afterEach过程中传递 }); it("Spec 1", function() { expect(this.testCount).toBe(1); }); it("Spec 2", function() { expect(this.testCount).toBe(1); }); }); })();
angular-mocks.js
inject
angular.module('myApplicationModule', []) .value('mode', 'app') .value('version', 'v1.0.1'); 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'); }); }); });
url:http://jasmine.github.io/2.0/introduction.html
https://docs.angularjs.org/api/ngMock