测试框架--mocha

$ npm install mocha --save
### 为了操作的方便,请在全局环境也安装一下Mocha

/**
 * Created by on 2017/5/16.
 */
const assert = require('assert');
const fs     = require('fs');

/**
 * supertest请求测试
 */
const superagent = require('superagent');
const app = require('../app');

function request() {
    return superagent(app.listen());
}


describe('Array', function() {
    describe('#indexOf()', function() {
        it('should return -1 when the value is not present', function() {
            assert.equal(0, [1,2,3].indexOf(1));
            assert.equal(-1, [1,2,3].indexOf(0));
        });
    });
});

describe('Boolean', function() {
    describe('#===', function() {
        it('如果 数值型200 全等于 字符型200,返回true', function() {
            assert.equal(false, 200 === '200');
        });
    });
});

/**   断言库组件---should.js
 *
 */
const should = require("should");
describe('Should test', function() {
    it('number', function() {
        (123).should.be.a.Number;
    });
    it('object property', function() {
        const obj = {name:'minghe',email:"[email protected]"};
        obj.should.have.property('name','minghe');
        obj.should.have.property('email');
    });
    it('ok',function () {
        true.should.be.ok;
    });

    it('equal',function () {
        'abc'.should.equal = 'abc';
    });
    it('not equal',function(){
        'abc'.should.not.equal('ddd');
    });
    it('exist',function(){
        const result = {};
        should.exist(result);
    })
});

describe('fs', function() {
    describe('#readFile()', function() {
        it('should not be null', function(done) {
            fs.readFile('../package.json', 'utf8', function(err,data){
                if (err){
                    return console.error(err);
                }
                data.should.not.equal(null);
                // console.log(data);
                done();
            });
        });
    });
});

/**
    superagent请求测试
 */


describe('Routes', function () {
    describe('GET /', function () {
        it('should return 200', function (done) {
            request()
                .get('/users')
                .expect('users', done); //done 是必须传入的,这样请求测试结束后,才能把测试信息推送给mocha处理
        });
    });
});

你可能感兴趣的:(测试框架--mocha)