jasmine

简介

jasmine 是一个测试驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器、dom或其他js框架

jasmine有十分简洁的语法

使用

这里 下载 stantd-alone安装包,并解压,双击打开里面的 specRunner.html, 即可看到测试示例,我们只要将自己的js源文件和测试文件分别放到 srcspec 文件夹中。

specRunner.html 到底是长什么样子的呢? f12 我们发现 先后加载了 jasmine.css, jasmine.js ,jasmine-html.js, boot.jsjasmine框架相关文件和 我们的 js源文件 jasmine测试文件

语法

分组 describe()

describe的作用是群组相关的测试, describe是可以嵌套的 从外到内 walkdown describe层级的 beforeEach, 由内到外walkup describe层级的 afterEach

  1. describe('a suite', function(){ //这是一个测试分组
  2. it('with an expactation',function(){
  3. expect(true).toBe(true);
  4. });
  5. });

测试it()

describe it 都是函数,所以它们可以包含任何js代码, 在 describe 中声明的变量和方法,能在 it中被访问。

it 代表的是具体的测试,当其中所有的断言都为true时,则该测试通过; 否则测试失败

  1. describe('a suit is just a function', function(){
  2. var a = 10;
  3. it('and here is a test', function(){
  4. var a = true;
  5. expect(a).toBe(true);
  6. });
  7. });

期望expect()

  1. desribe('the "toBe" matcher compares with "===" ', function(){
  2. it('positive expect', function(){
  3. expect(true).toBe(true);
  4. });
  5. it('negative expect', function(){
  6. expect(false).not.toBe(true);
  7. });
  8. });

匹配 to*()

每个匹配方法在期望值和实际值之间执行逻辑比较,它负责告诉jasmine断言的真假,从而决定测试的成功或失败。

肯定断言 expect(true).toBe(true);

否定断言 expect(false).not.toBe(true);

jasmine有很丰富的匹配方法,而且可以自定义匹配方法。 内置的匹配方法有:

  • toBe()

  • toEqual()

  • toMatch()

  • toBeUndefined()

  • toBeNull()

  • toBeTruthy()

  • toContain()

  • toBeLessThan()

  • toBeCloseTo()

  • toThrowError()

    1. describe("included matchers", function(){
    2. it('"toBe" matcher compares width === ', function(){
    3. var a = 12;
    4. var b = a;
    5. expect(a).toBe(b);
    6. expect(a).not.toBe(null);
    7. });
    8. describe('"toEqual" matcher', function(){
    9. it('work for simple literals and variable', function(){
    10. var a = 12;
    11. expect(a).toEqual(12);
    12. });
    13. it('should work for objects', function(){
    14. var foo = {
    15. a: 12,
    16. b: 23
    17. };
    18. var bar = {
    19. a: 12,
    20. b: 23
    21. };
    22. expect(foo).toEqual(bar); //true?
    23. });
    24. });
    25. it('"toMatch" matcher is for regular expression', function(){
    26. var message = "foo bar baz";
    27. expect(message).toMatch(/bar/);
    28. expect(message).toMatch("bar");
    29. expect(message).not.toMatch(/quue/);
    30. });
    31. it('"toBeUndefined" matcher compares against "undefined"', function(){
    32. var a = {
    33. foo: "foo"
    34. };
    35. expect(a.foo).not.toBeUndefined();
    36. expect(a.bar).toBeUndefined();
    37. });
    38. it(' "toBeNull" matcher compares against "null"', function(){
    39. var a = null;
    40. var foo = 'foo';
    41. expect(null).toBeNull();
    42. expect(a).toBeNull();
    43. expect(foo).not.toBeNull();
    44. });
    45. it('"toBeTruthy" matcher is for boolean casting testing' , function(){
    46. var a, foo = 'foo';
    47. expect(foo).toBeTruthy();
    48. expect(a).not.toBeTruthy();
    49. });
    50. it('"toContain" matcher is for finding an item in an array', function(){
    51. var a = ['foo', 'bar', 'baz'];
    52. expect(a).toContain('bar');
    53. expect(a).not.toContain('quu');
    54. });
    55. it('"toBeLessThan" matcher is for math comparisons', function(){
    56. var n = 2.23, e = 1.33;
    57. expect(e).toBeLessThan(n);
    58. expect(n).not.toBeLessThan(e);
    59. });
    60. it('"toBeCloseTo" matcher is for precision match comparison', function(){
    61. var n = 1.99, e = 2.35;
    62. expect(e).not.toBeCloseTo(n, 2);
    63. expect(e).toBeCloseTo(n, 0);
    64. });
    65. it('"toThrowError" matcher is for testing a specific thrown exception', function(){
    66. var foo = function(){
    67. throw new TypeError('foo bar baz');
    68. };
    69. expect(foo).toThrowError('foo bar baz');
    70. expect(foo).toThrowError(/bar/);
    71. expect(foo).toThrowError(TypeError);
    72. expect(foo).toThrowError(TypeError, 'foo bar baz');
    73. });
    74. });

设置和清理 beforeEach(), afterEach()

beforeEach() 在它所属的 describe 块中的每条测试执行前,先执行的js代码, 作用就是设置和初始化一些东西。

afterEach()beforeEach 相反,在 describe 块的每条测试执行后执行, 做一些清理的工作。

  1. describe('tests with "setup" and "tear-down"', function(){
  2. var foo;
  3. beforeEach(function(){
  4. foo = 0;
  5. foo += 1; //每次测试前都初始化 foo == 1
  6. });
  7. afterEach(function(){
  8. foo = 0; //每次测试完都重置 foo = 0;
  9. });
  10. it('it is just a function , so can contain any code', function(){
  11. expect(foo).toEqual(1);
  12. });
  13. it('can have more than one expectation', function(){
  14. expect(foo).toEqual(1);
  15. expect(true).toEqual(true);
  16. });
  17. });

this对象

另一种在 beforeEach afterEach it 之间共享变量的方法: this对象, 每次执行完1条测试之后,this 都会被重置为空对象

  1. describe('a suite', function(){
  2. beforeEach(function(){
  3. this.foo = 0;
  4. });
  5. it('can use "this" to share initial data', function(){
  6. expect(this.foo).toEqual(0);
  7. this.bar = "test pollution?";
  8. });
  9. it('prevents test pollution by having an empty "this" created for next test', function(){
  10. expect(this.foo).toEqual(0);
  11. expect(this.bar).toBe(undefined);
  12. });
  13. });

describe嵌套 beforeEach串行

  1. describe('a suite', function(){
  2. var foo;
  3. beforeEach(function(){
  4. foo = 0;
  5. foo += 1;
  6. });
  7. afterEach(function(){
  8. foo = 0;
  9. });
  10. it('a spec', function(){
  11. expect(foo).toEqual(1);
  12. });
  13. it('a spec too', function(){
  14. expect(foo).toEqual(1);
  15. expect(true).toEqual(true);
  16. });
  17. describe('nested inside describe', function(){
  18. var bar;
  19. beforeEach(function(){
  20. bar = 1;
  21. });
  22. // exec outer's describe beforeEach > this describe's beforeEach
  23. it('可以访问外部describe的beforeEach的变量', function(){
  24. expect(foo).toEqual(bar);
  25. });
  26. });
  27. });

禁用describe或it

xdescribe(), xit()pending()

  1. xdescribe('a suite',function(){
  2. //will not execute
  3. });
  4. describe('a suite too', function(){
  5. xit('this test be canceled', function(){
  6. expect(true).toBe(false);
  7. });
  8. it('can be desclared with "it" but without a function');
  9. if('can be declared by calling "pending()" in spec body', function(){
  10. expect(true).toBe(false);
  11. pending(); //禁用该测试
  12. });
  13. });

函数调用监听 spy

spyOn() , toHaveBeenCalled() , toHaveBeenCalledWith()

  1. describe('a spy', function(){
  2. var foo, bar = null;
  3. beforeEach(function(){
  4. foo = {
  5. setBar = function(value){
  6. bar = value;
  7. };
  8. };
  9. spyOn(foo, 'setBar');
  10. foo.setBar(123);
  11. foo.setBar(456, 'another param');
  12. });
  13. it('tracks that the spy was called', function(){
  14. expect(foo.setBar).toHaveBeenCalled();
  15. });
  16. it('tracks all the arguments of its calls', function(){
  17. expect(foo.setBar).toHaveBeenCalledWith(123);
  18. expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
  19. });
  20. it('stops all execution on a function', function(){
  21. expect(bar).toBeNull(); //setBar函数的执行 被spy监听器暂停了。
  22. });
  23. });
  24. describe('a spy, when configured to call through', function(){
  25. var foo , bar, fetchedBar;
  26. beforeEach(function(){
  27. foo = {
  28. setBar: function(value){
  29. bar = value;
  30. },
  31. getBar: function(){
  32. return bar;
  33. }
  34. };
  35. spyOn(foo, 'getBar').and.callThrough();
  36. foo.setBar(123);
  37. fetchedBar = foo.getBar();
  38. });
  39. it('tracks that the spy was called', function(){
  40. expect(foo.getBar).toHaveBeenCalled();
  41. });
  42. it('should not effect other function', function(){
  43. expect(bar).toEqual(123);
  44. });
  45. it('when called returns the requested value' , function(){
  46. expect(fetchedBar).toEqual(123);
  47. })
  48. });

jasmine note的更多相关文章

  1. Jasmine

    Jasmine https://www.npmjs.com/package/jasmine The Jasmine Module The jasmine module is a package of ...

  2. 前端单元测试环境搭建 Karma Jasmine

    Karma 官网On the AngularJS team, we rely on testing and we always seek better tools to make our life e ...

  3. 三星Note 7停产,原来是吃了流程的亏

    三星Note 7发售两个月即成为全球噩梦,从首炸到传言停产仅仅47天.所谓"屋漏偏逢连天雨",相比华为.小米等品牌对其全球市场的挤压.侵蚀,Galaxy Note 7爆炸事件这场连 ...

  4. 《Note --- Unreal --- MemPro (CONTINUE... ...)》

    Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...

  5. 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》

    ---------------------------------------------------------------------------------------------------- ...

  6. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  7. Beginning Scala study note(9) Scala and Java Interoperability

    1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...

  8. Beginning Scala study note(8) Scala Type System

    1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...

  9. Beginning Scala study note(7) Trait

    A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...

随机推荐

  1. JAVA设计原则

      1.单一职责规则(SRP):有且仅有一个原因引起类的变化. 2.依赖倒置规则(DIP): 高层模块不应该依赖底层模块,两者都用依赖接口: 接口不依赖细节: 细节依赖接口. 3.接口隔离规则:类间的 ...

  2. python dict traversal

    rs=dict() rs['item1'] = 'item1' rs['item2'] = 'item2' for k,d in rs.items(): print k,d for k in rs.k ...

  3. SQL复杂查询(子查询)

    USE STUDY SELECT * from EMP SELECT * FROM SALGRADE --1.查询雇员姓名,所在部门编号和名称 SELECT ename,EMP.deptno,DEPT ...

  4. Android Studio 添加Assets目录

    Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...

  5. c语言面试题(感觉比较好的题目)

    1.static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别? 答:static全局变量--只在定义了该变量的源文件内有效,初 ...

  6. win7_32位安装MySQL_5.6以及密码修改方法

    1.下载mysql: http://www.xiazaiba.com/html/361.html 2.安装 方便起见,全部默认下一步吧,原理一个样,最后安装到: 3.配置环境变量 我这里添加的是  C ...

  7. HBuilder的几个常用快捷键

    Alt + [ 匹配括号 Alt + ↓跳转到下一个可编辑区 Ctrl + Alt + j 合并下一行 Ctrl + Alt + ←选择助手 Shift + 回车 Shift + 空格   Ctrl ...

  8. C语言入门(9)——局部变量与全局变量

    变量有效性的范围称变量的作用域.C语言中所有的量都有自己的作用域.变量说明的方式不同,其作用域也不同. C语言中的变量,按作用域范围可分为两种,即局部变量和全局变量.   局部变量 局部变量也称为内部 ...

  9. Java程序员快速入门Go语言

    这篇文章帮助Java程序员快速入门Go语言. 转载至 开源中国社区. http://www.oschina.net 本文将以一个有代表性的例子为开始,以此让Java程序员对Go语言有个初步认识,随后将 ...

  10. poj2823_单调队列简单入门

    题目链接:http://poj.org/problem?id=2823 #include<iostream> #include<cstdio> #define M 100000 ...