码迷,mamicode.com
首页 > 其他好文 > 详细

node相关--测试

时间:2014-11-30 18:32:58      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   使用   sp   java   

测试:

  • assert模块; //node自带
    var assert = require(‘assert‘);
     
    var now = Date.now();
    console.log(now);
    assert.ok(now % 2 == 0);
    
    
    ----------------------------------------
    var request = require(‘superagent‘);
    var assert = require(‘assert‘);
     
    request.get(‘http://localhost:3000‘)
           .send({q: ‘bieber‘})
           .end(function (res) {
            //断言判断响应状态码是否正确
            assert.ok(200 == res.status);
            //断言关键字是否存在
            assert.ok(~res.text.toLowerCase().indexOf(‘bieber‘));
            //断言列表项是否存在
            assert.ok(~res.text.indexOf(‘<li>‘));
           });
  • ecpect.js,优化assert的代码书写方式; API:
    • ok:断言是否为真:
      expect(1).to.be.ok();
      expect(true).to.be.ok();
      expect({}).to.be.ok();
      expect(0).to.not.be,ok();
    • be/equal: === 

      expect(1).to.be(1);
      expect(NaN).not.to.equal(NaN);
      expect(1).not.to.be(true);
    • eql:断言非严格相等,支持对象

      expect({a: ‘b‘}).to.eql({a: ‘b‘});
      expect(1).to.eql(‘1‘);
    • a/an:断言所属属性类型,支持数组的instanceof
      expect(5).to.be.a(‘number‘);
      expect([]).to.be.an(‘array‘);
      expect([]).to.be.an(‘object‘);
      //constructor
      expect(5).to.be.a(‘Number‘);
      expect([]).to.be.an(‘Array‘);
      expect(tobi).to.be.a(Ferrect); //instanceof
    • match:断言字符串是否匹配一段正则表达式
      expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
    • contain:断言字符串是否包含另一个字符串;
      expect([1,2]).to.contain(1);
      expect(‘hello world‘).to.contain(‘world‘);
    • length:断言数组长度;
      expect([]).to.have.length(0);
      expect([1,2,3]).to.have.length(3);
    • empty:断言数组是否为空;
      expect([]).to.be.empty();
      expect([1,2,3]).to.not.be.empty();
    • property:断言某个自身属性/值是否存在;
      expect(window).to.have.property(‘expect‘);
      expect(window).to.have.property(‘expect‘,expect);
      expect({a: ‘b‘}).to.have.property(‘a‘);
    • key/keys:断言键是否存在,支持only修饰符;
      expect({a: ‘b‘}).to.have.key(‘a‘);
      expect({a: ‘b‘, c: ‘d‘}).to.only.have.keys(‘a‘, ‘c‘);
      expect({a: ‘b‘, c: ‘d‘}).to.only.have.keys([‘a‘.‘c‘]);
      expect({a: ‘b‘, c: ‘d‘}).to.not.only.have.keys(‘a‘);
    • throwException:断言Function在调用时是否会抛出异常;
      expect(fn).to.throwException();
      expect(fn2).to.not.throwException();
    • within:断言数组是否在某一个区间内;
      expect(1).to.be.within(0,Infinity);
    • greaterThan/above:   >
      expect(3).to,be.above(0);
      expect(5).to.be.greaterThan(3);
    • lessThan/below: <
      expect(0).to.be.below(3);
      expect(1).to.be.lessThan(3);

Moncha: 测试框架

  • 例子:
    • test.js
      describe(‘a topic‘, function () {
          it(‘should test something‘, function () {
       
          });
          describe(‘anthor topic‘, function () {
              it(‘should test something else‘, function () {
                   
              })
          })
      });
    • 运行:mocha test.js   ;报告列表形式:  mocha -R list test.js
  • 测试异步代码:Mocha默认在一个测试用例执行之后立即执行另一个;但有时候希望延缓下一个测试用例的执行;
    it(‘should not know‘, function (done) {
       setTimeout(function () {
          assert.ok(1 == 1);
          done();
       }, 100);   
    });
    

      如果一个测试用例中有许多异步操作,可以添加一个计数器:

    it(‘should complete three requests‘, function (done) {
      var total = 3;
      request.get(‘http://localhost:3000/1‘, function (res) {
         if(200 != res.status) throw new Error(‘Request error‘); --total || done();
      });
      request.get(‘http://localhost:3000/2‘, function (res) {
         if(200 != res.status) throw new Error(‘Request error‘); --total || done();
      });
      request.get(‘http://localhost:3000/3‘, function (res) {
         if(200 != res.status) throw new Error(‘Request error‘); --total || done();
      });
    })
  • BDD风格: 前面的测试例子风格为BDD(行为驱动开发);
  • TDD风格: 测试驱动开发,组织方式是使用测试集(suit)和测试(test);每个测试集都有setup和teardowm函数,这些方法会在测试集中的测试执行前执行,为了避免代码重复已经最大限度使得测试之间相互独立;
    suite(‘net‘, function () {
       suite(‘Stream‘, function () {
          var  client;
          suiteSetup(function () {
             client = net.connect(3000, ‘localhost‘);
          });
     
          test(‘connect event‘, function (done) {
             client.on(‘connect‘, done);
          });
     
          test(‘receiving data‘, function (done) {
            client.write(‘‘);
            client.once(‘data‘, done);
          });
     
          suiteTeardown( function () {
            client.end();
          })
       })
    })
  • export风格:使用node模块系统来输出测试;每个export的键都表示测试集,嵌套的测试集可以用子对象来表示
    exports.Array = {
       ‘#indecOf()‘ : {
          ‘should return -1 when the value is not present‘ : function () {},
          ‘should return the correct index when the value is present‘ : function () {}
       }
    }

在浏览器端使用Mocha:

node相关--测试

标签:des   blog   http   io   ar   os   使用   sp   java   

原文地址:http://www.cnblogs.com/jinkspeng/p/4133441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!