1 var config = require('../config/endpoints'), 2 request = require('supertest')(config.host[config.env]); 3 4 5 describe('測試訪問baidu首頁.', function() { 6 it('返回狀態碼爲200.', function(done) { 7 request.get('/') 8 .expect(200) 9 .end(done); 10 }); 11 12 });
➜ api-test git:(master) ✗ grunt Running "mochaTest:test" (mochaTest) task 測試訪問baidu首頁. ✓ 返回狀態碼爲200. (958ms) 1 passing (964ms) Done, without errors.
1 var config = require('../config/endpoints'), 2 request = require('supertest')(config.host[config.env]); 3 4 5 describe('測試訪問baidu首頁.', function() { 6 it('返回狀態碼爲200.', function(done) { 7 request.get('/') 8 //此處失敗 9 .expect(201) 10 .end(done); 11 }); 12 13 });
➜ api-test git:(master) ✗ grunt Running "mochaTest:test" (mochaTest) task 測試訪問baidu首頁. 1) 返回狀態碼爲200. 0 passing (2s) 1 failing 1) 測試訪問baidu首頁. 返回狀態碼爲200.: Error: expected 201 "Created", got 200 "OK" Warning: Task "mochaTest:test" failed. Use --force to continue. Aborted due to warnings.
request.get('/')
.set('Content-Type','application/json')
1 request 2 .post('/post/data') 3 .send({ 4 "A": 1, 5 "B": 2 6 })
.expect(function(res){ //返回中totalCount爲6 if (!(res.body.totalCount.should.equal(6))) throw new Error("總數不對"); })
總結-測試規劃git