端到端测试(一)

零散的笔记。javascript

6. Jasmine

Jasmine是一个用于测试JavaScript代码的行为驱动开发框架。java

6.1 细则套件(describe()

Jasmine套件的核心部分是describe函数。正则表达式

describe('Unit test: MainController', function() { });
  • 1
  • 2

describe()函数带有两个参数,一个字符串(细则套件名称、描述),一个函数(封装了测试套件) 
可嵌套。数组

6.2 定义一个细则(it()

经过调用it()函数来定义一个细则. 
it()函数带有两个参数:一个字符串(是细则的标题或者描述),一个函数,包含了一个或多 
个用于测试代码功能的预期(expect())。 
一个全部预期都为true的测试就算是一条 
经过的细则.框架

describe('A spec suite', function() { it('contains a passing spec', function() { expect(true).toBe(true); }); });

7. 预期(expect)

使用expect()函数来创建预期。 
expect()函数带有一个单值参数。这个参数被称为真实值。 
要创建一个预期,咱们给它串联一个带单值参数的匹配器函数(like toBe()),这个参数就是指望值。 
能够经过在调用匹配器以前 
调一个not来建立测试的否认式。函数

describe('A spec suite', function() { it('contains a passing spec', function() { expect(true).toBe(true); }); it('contains another passing spec', function() { expect(false).not.toBe(true); }); });

 

7.1 内置的匹配器

  1. toBe
  2. toEqual
  3. toMatch(正则匹配)
  4. toBeDefined
  5. toBeUndefined
  6. toBeNull
  7. toBeTruthy
  8. toBeFalsy
  9. toContain
  10. toBeLessThan
  11. toBeGreaterThan
  12. toBeCloseTo(指定精度内)
  13. toThrow(异常) 、
  14. 建立自定义匹配器 
    调用addMatcher()函数,带入一个值:
describe('A spec suite', function() { this.addMatchers({ toBeLessThanOrEqual: function(expected) { return this.actual <= expected; } }); });

具体参考:http://blog.csdn.net/u012223913/article/details/50337401



expect(x).toEqual(y); 当x和y相等时候经过测试

expect(x).toBe(y); 当x和y是同一个对象时候经过ui

expect(x).toMatch(pattern); x匹配pattern(字符串或正则表达式)时经过this

expect(x).toBeDefined(); x不是undefined时经过lua

expect(x).toBeUndefined(); x 是 undefined时经过

expect(x).toBeNull(); x是null时经过

expect(x).toBeTruthy(); x和true等价时候经过

expect(x).toBeFalsy(); x和false等价时候经过

expect(x).toContain(y);x(数组或字符串)包含y时经过

expect(x).toBeLessThan(y); x小于y时经过

expect(x).toBeGreaterThan(y); x大于y时经过

expect(function(){fn();}).toThrow(e); 函数fn抛出异常时候经过

相关文章
相关标签/搜索