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

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

测试it()

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

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

describe('a suit is just a function', function(){
var a = 10;
it('and here is a test', function(){
var a = true;
expect(a).toBe(true);
});
});

期望expect()

desribe('the "toBe" matcher compares with "===" ', function(){
it('positive expect', function(){
expect(true).toBe(true);
}); it('negative expect', function(){
expect(false).not.toBe(true);
});
});

匹配 to*()

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

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

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

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

  • toBe()

  • toEqual()

  • toMatch()

  • toBeUndefined()

  • toBeNull()

  • toBeTruthy()

  • toContain()

  • toBeLessThan()

  • toBeCloseTo()

  • toThrowError()

      describe("included matchers", function(){
    it('"toBe" matcher compares width === ', function(){
    var a = 12;
    var b = a;
    expect(a).toBe(b);
    expect(a).not.toBe(null);
    }); describe('"toEqual" matcher', function(){
    it('work for simple literals and variable', function(){
    var a = 12;
    expect(a).toEqual(12);
    }); it('should work for objects', function(){
    var foo = {
    a: 12,
    b: 23
    };
    var bar = {
    a: 12,
    b: 23
    }; expect(foo).toEqual(bar); //true?
    });
    }); it('"toMatch" matcher is for regular expression', function(){
    var message = "foo bar baz";
    expect(message).toMatch(/bar/);
    expect(message).toMatch("bar");
    expect(message).not.toMatch(/quue/);
    }); it('"toBeUndefined" matcher compares against "undefined"', function(){
    var a = {
    foo: "foo"
    }; expect(a.foo).not.toBeUndefined();
    expect(a.bar).toBeUndefined();
    }); it(' "toBeNull" matcher compares against "null"', function(){
    var a = null;
    var foo = 'foo'; expect(null).toBeNull();
    expect(a).toBeNull();
    expect(foo).not.toBeNull();
    }); it('"toBeTruthy" matcher is for boolean casting testing' , function(){
    var a, foo = 'foo';
    expect(foo).toBeTruthy();
    expect(a).not.toBeTruthy();
    }); it('"toContain" matcher is for finding an item in an array', function(){
    var a = ['foo', 'bar', 'baz'];
    expect(a).toContain('bar');
    expect(a).not.toContain('quu');
    }); it('"toBeLessThan" matcher is for math comparisons', function(){
    var n = 2.23, e = 1.33;
    expect(e).toBeLessThan(n);
    expect(n).not.toBeLessThan(e);
    }); it('"toBeCloseTo" matcher is for precision match comparison', function(){
    var n = 1.99, e = 2.35;
    expect(e).not.toBeCloseTo(n, 2);
    expect(e).toBeCloseTo(n, 0);
    }); it('"toThrowError" matcher is for testing a specific thrown exception', function(){
    var foo = function(){
    throw new TypeError('foo bar baz');
    };
    expect(foo).toThrowError('foo bar baz');
    expect(foo).toThrowError(/bar/);
    expect(foo).toThrowError(TypeError);
    expect(foo).toThrowError(TypeError, 'foo bar baz');
    });
    });

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

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

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

describe('tests with "setup" and "tear-down"', function(){
var foo;
beforeEach(function(){
foo = 0;
foo += 1; //每次测试前都初始化 foo == 1
}); afterEach(function(){
foo = 0; //每次测试完都重置 foo = 0;
}); it('it is just a function , so 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);
});
});

this对象

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

describe('a suite', function(){
beforeEach(function(){
this.foo = 0;
}); it('can use "this" to share initial data', function(){
expect(this.foo).toEqual(0);
this.bar = "test pollution?";
}); it('prevents test pollution by having an empty "this" created for next test', function(){
expect(this.foo).toEqual(0);
expect(this.bar).toBe(undefined);
});
});

describe嵌套 beforeEach串行

describe('a suite', function(){
var foo;
beforeEach(function(){
foo = 0;
foo += 1;
});
afterEach(function(){
foo = 0;
}); it('a spec', function(){
expect(foo).toEqual(1);
});
it('a spec too', function(){
expect(foo).toEqual(1);
expect(true).toEqual(true);
}); describe('nested inside describe', function(){
var bar; beforeEach(function(){
bar = 1;
}); // exec outer's describe beforeEach > this describe's beforeEach
it('可以访问外部describe的beforeEach的变量', function(){
expect(foo).toEqual(bar);
});
});
});

禁用describe或it

xdescribe(), xit()pending()

xdescribe('a suite',function(){
//will not execute
}); describe('a suite too', function(){
xit('this test be canceled', function(){
expect(true).toBe(false);
}); it('can be desclared with "it" but without a function'); if('can be declared by calling "pending()" in spec body', function(){
expect(true).toBe(false);
pending(); //禁用该测试
});
});

函数调用监听 spy

spyOn() , toHaveBeenCalled() , toHaveBeenCalledWith()

describe('a spy', function(){
var foo, bar = null;
beforeEach(function(){
foo = {
setBar = function(value){
bar = value;
};
}; spyOn(foo, 'setBar'); foo.setBar(123);
foo.setBar(456, 'another param');
}); it('tracks that the spy was called', function(){
expect(foo.setBar).toHaveBeenCalled();
}); it('tracks all the arguments of its calls', function(){
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
}); it('stops all execution on a function', function(){
expect(bar).toBeNull(); //setBar函数的执行 被spy监听器暂停了。
});
}); describe('a spy, when configured to call through', function(){
var foo , bar, fetchedBar; beforeEach(function(){
foo = {
setBar: function(value){
bar = value;
},
getBar: function(){
return bar;
}
}; spyOn(foo, 'getBar').and.callThrough(); foo.setBar(123);
fetchedBar = foo.getBar();
}); it('tracks that the spy was called', function(){
expect(foo.getBar).toHaveBeenCalled();
}); it('should not effect other function', function(){
expect(bar).toEqual(123);
}); it('when called returns the requested value' , function(){
expect(fetchedBar).toEqual(123);
})
});

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. House Robber & House Robber II

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  2. xcode7 没有Empty Application

    如果你想创建xcode6.01之前版本提供的空工程,其实很简单.1:选择模板 Single View Application2:选中 Main.storyboard,将其删除3:选择项目的 plist ...

  3. Linux命令之ifconfig

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  4. 工作备份 build.gradle

    apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion '22.0.0' de ...

  5. android 添加左右滑屏手势

    今天要在自己的项目中添加左右滑动,实现日期的加减(原来已经做了加减按键).滑动在一个中间的layout中进行 思路:添加左右划屏幕判断,得到判断后模拟加减按键按下. 模拟按键按下用 mbotton.p ...

  6. 读配置文件 properties

    /** * */package com.sprucetec.tms.fee.utils;import java.io.IOException;import java.util.ArrayList;im ...

  7. java积累

    数组的使用 package javaDemo; import java.util.*; /** * * @author Administrator * @version 1.0 * * */ publ ...

  8. 记录一下八款开源 Android 游戏引擎

    记录一下八款开源 Android 游戏引擎 虽然android学了点点,然后现在又没学了(我为啥这么没有恒心呢大哭).以后有时间还是要继续学android的,一定要啊!虽然现在没学android游戏编 ...

  9. JAVA Layout

    /**  * baidu :组件不会直接放到框架上,而是放在若干个面板上,这些面板再放到窗格上?  * 实际上在JFrame上可直接添加Jbutton  *   * BorderLayout Flow ...

  10. nodejs微信开发获取token,ticket-1

    /* jshint -W079 */ /* jshint -W020 */ "use strict"; var _ = require("lodash"); v ...