Mocha 从0开始

Mocha

Mocha 是具有丰富特性的 JavaScript 测试框架,可以运行在 Node.js 和浏览器中,使得异步测试更简单更有趣。Mocha 可以持续运行测试,支持灵活又准确的报告,当映射到未捕获异常时转到正确的测试示例。

Sinon 是一个独立的 JavaScript 测试spy, stub, mock库,没有依赖任何单元测试框架工程。

assert模块

搬运自
http://javascript.ruanyifeng.com/nodejs/assert.html#toc2


var assert = require('assert');
//assert方法接受两个参数,当第一个参数对应的布尔值为true时,不会有任何提示,返回undefined。当第一个参数对应的布尔值为false时,会抛出一个错误,该错误的提示信息就是第二个参数设定的字符串。
assert.equal(Math.max(1,100),100, '预期相等');


// 以下三句效果相同
assert(Math.max(1,100) == 100, '预期等于100');
assert.ok(Math.max(1,100) == 100, '预期等于100');
assert.equal(Math.max(1,100), 100, '预期等于100');


//预期代码会抛出一个错误
//如果没有抛出错误反而认为是有问题的
assert.throws(
  function() {
    if(add(1,2) == 3){
      throw new Error('add result wrong');
    }
  },
  /add/,
  'add有问题'
);

mocha 第一步

mocha需要全局安装 一会儿我们需要用到mocha命令
之后可以通过mocha xxx.js 来执行测试

或者把待测试js都放在test文件夹中
之后在和test的同级目录运行mocha命令(推荐)

一个简单的mocha例子

var assert = require("assert");

//测试Array中的indexOf


describe('Array', function() {
  describe('#indexOf()', function() {
    //具体的测试语句会放在it的回调函数里
    //
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1, 2, 3].indexOf(5));
      assert.equal(-1, [1, 2, 3].indexOf(0));
    })
  })

describe (moduleName, testDetails)
describe可以嵌套 module_name 是可以随便取的

it (info, function)
具体的测试语句会放在it的回调函数里,一般来说info字符串会写期望的正确输出的简要一句话文字说明
当该it block内的test failed的时候控制台就会把详细信息打印出来

异步测试

参考
https://lostechies.com/derickbailey/2012/08/17/asynchronous-unit-tests-with-mocha-promises-and-winjs/
异步的测试有什么不同呢? 如果你在测试异步的代码 你并不知道异步的代码什么时候完成

你天真的会以为是这样 (这里使用的Chai来作断言)

describe("a test", function(){
  var foo = false;
  beforeEach(function(){
    setTimeout(function(){
      foo = true;
    }, 50);
  });

  it("should pass", function(){
    expect(foo).equals(true);
  });

});

实际上应该这样

describe("a test", function(){
  var foo = false;
  beforeEach(function(done){
    setTimeout(function(){
      foo = true;
      done();
    }, 500);
  });
  it("should pass", function(){
    expect(foo).equals(true);
  });
});

使用Promise的异步

describe("a test", function(){
  var foo = false;
  beforeEach(function(done){
    var promise = new Promise(function(resolve){
      resolve(true);
    });
    promise.then(function(value){
      // get the value from the completed promise
      foo = value;
      // complete the async beforeEach
      done();
    });
  });
  it("should pass", function(){
    expect(foo).equals(true);
  });
});

Mocha的Assertions

Mocha官网上关于mocha可用的assertion列举了很多断言库

  • should.js BDD风格
    API
var should = require('should');
should('abc').be.a.String();

var a = { a: 10 }, b = Object.create(null);
b.a = 10;

a.should.be.eql(b);
  • expect.js based on should.js
expect(window.r).to.be(undefined);
expect({ a: 'b' }).to.eql({ a: 'b' })
expect(5).to.be.a('number');
expect([]).to.be.an('array');
expect(window).not.to.be.an(Image);

demos

  • Chai
    Chai 是一个针对 Node.js 和浏览器的行为驱动测试和测试驱动测试的断言库,可与任何 JavaScript 测试框架集成。
    它拥有 should expect assert 三种模式
    访问Official Site即可看到

比如expect

var expect = chai.expect;

expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(tea).to.have.property('flavors').with.length(3);
  • better-assert
  assert('tobi' == user.name);
  assert('number' == typeof user.age);
posted @ 2015-11-07 23:03  cart55free99  阅读(260)  评论(0编辑  收藏  举报