Node関連--テスト

6128 ワード

テスト:
  • 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のデフォルトは、1つのテスト・インスタンスが実行された直後に別のコードを実行します.しかし、次のテスト例の実行を遅らせたい場合があります.
    it('should not know', function (done) {
       setTimeout(function () {
          assert.ok(1 == 1);
          done();
       }, 100);   
    });
    

    1つのテスト・インスタンスに多くの非同期操作がある場合は、カウンタを追加できます.
    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の使用: