nodejswebapi單元測試

目錄

目錄 packages 1 mocha 2 supertest 3 istanbul demo 1 code 2 run test

1. packages

1.1 mocha

最廣泛使用的js測試框架

npm install -g mocha
或者隻在項目的開發環境下安裝
npm install --save-dev mocha

1.2 supertest

http代理服務引擎,用來模擬http請求測試nodejs 接口

npm install --save-dev supertest

1.3 istanbul

用來生成測試用例覆蓋率報表

npm install -g istanbul

2. demo

2.1 code

這裡保存瞭登錄之後的cookie,後面的請求都需要。
嘗試用過agent = request.agent(app)的辦法but not work

var should = require('should');
var assert = require('assert');
var request = require('supertest');

describe('Routing', function() {
  var url = 'https://localhost:3001';
  var userCookie = '';

  before(function(done) {
   done();
  });

  describe('#login', function() {
    it('should login success', function(done){
      var profile = {
        email: 'xuyawenwen',
        password: '123456',
      };
      request(url)
        .post('/login')
        .send(profile)
        .expect('Content-Type', /json/)
        .expect(200) //Status code
        .end(function(err,res) {
          if (err) {
            throw err;
          }
          console.log("success res==>",res.body);
          // Should.js fluent syntax applied
          res.body.status.should.equal(0);
          userCookie = res.headers['set-cookie'];
          done();
        });
    });
  });

  describe('#account', function() {
    it('should return account info', function(done){
      request(url)
        .get('/account/info')
        .set('Cookie', userCookie)
        .expect('Content-Type', /json/)
        .expect(200) //Status code
        .end(function(err,res) {
          if (err) {
            throw err;
          }
          console.log("success res==>",res.body);
          // Should.js fluent syntax applied
          res.body.status.should.equal(0);
          done();
        });
    });
  });
});

2.2 run test

command

mocha src/server/modules/account/test.js
或者
istanbul cover _mocha src/server/modules/account/test.js 

result

Routing
    #login
success res==> { status: 0,
  result: 
   { url: '/product/overview',
     userToken: '12b1d0f503125f0c582f494f080443c6' } }
      ? should login success (394ms)
    #account
success res==> { status: 0,
  result: 
   { spendSummary: 
      { totalUsedData: 144582296,
        times: 33,
        usersCount: 2,
        totalCost: 0 },
     accountInfo: 
      { balance: 11262,
        dataUsage: 107192452412,
        accountId: '436806e0-ab16-11e6-a3b1-7d052e670cc0',
        planInfo: [Object] } } }
      ? should return account info (85ms)


  2 passing (489ms)

=============================================================================
Writing coverage object [/Users/steven/mediaexpress/coverage/coverage.json]
Writing coverage reports at [/Users/steven/mediaexpress/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements   : 92.31% ( 24/26 )
Branches     : 50% ( 2/4 )
Functions    : 100% ( 8/8 )

發佈留言