node测试基础

前言

到了开发的后期,测试工作往往是重中之重,但是测试本身又十分繁琐与复杂。对于使用js的朋友,对于我接下来要讲的内容绝对不会陌生,对,就是大名鼎鼎的摩卡!由于为我的好友开发的后端框架Zeta做测试工作,所以开始接触这一类测试工具和测试流程,就把我的学习经验分享给大家吧。

mocha

mocha的使用方法十分简单,就是两个语句describe和it

describe
jsdescribe('req.get',function(){
    describe('res.json',function(){
    });
});

被describe的回调所包裹的是一个测试流程,使用describe可以很好地为测试表明目的和区分层次

it
jsdescribe('Array.index',function(){
    it('should return -1 when not found',function(){
        var tmp=[1,2,3];
        tmp.indexOf(4).should.equal(-1);
    });
});

it的第一个参数是个字符串,你可以把它看做是你测试样例的期望结果,其实没有什么实际意义,不会对测试样例的运行有什么影响。
上面的例子是对于同步的,如果有异步回调怎么办呢?

jsdescribe('db.save',function(){
    it('should save the doc',function(done){
        doc.save(function(err,doc){
            if(err) done(err);
            if(!doc) done(err);
            done();
        });
    });
});

done是链的最后一个步骤,调用done既不会往下执行,done(err)则说明失败。

chai

chai是一个断言工具,提供了一些比较受欢迎的断言写法,这里主要介绍两个,should和expect。

should
jsvar should=require('chai').should();
res.text.should.equal('hi,world');
req.path.should.include('users');
foo.should.have.length(3);

从上面的例子大家可以领会should的写法了。

expect
jsvar expect=require('chai').expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(beverages).to.have.property('tea').with.length(3);

chai的使用十分人性化,符合自然语言的规律,就不多提了。

supertest

对后端测试的时候难免会发起请求,supertest为我们提供了这样的功能。

jsvar request=require('supertest');
request(app).
    get('/foo').
    expect(200).
    expect('Content-Type','application/json').
    end(function(err,res){
        if(err) throw err;
    });

要模拟异步ajax怎么办呢

js//json上传
request(app).
    post('/foo').
    send({key:'value'}).
    expect(200);
//表单提交
request(app).
    post('/foo').
    type('form').
    send({key:value});
//上传文件
request(app).
    post('/foo').
    attach('field','filepath').
    ....

要设置一些东西怎么办呢?

jsrequest(app).
    post('/hh').
    send({key:val}).
    set('Accept-laguange','zh-cn').
    set('Cookie',['user=suemi','passwd=*******']).
    ...

supertest主要使用的就是expect和end了,通过例子,大家也很清楚基本的用法了,对于详细的API可以参考官网。

完整示例

最后给大家带来一个我自己写的完整示例。

jsvar Zeta=require('../../'),
    assert=require('assert'),
    request=require('supertest'),
    demo=Zeta.module('demo',[]),
    should=require('chai').should();
demo.load();

describe('singleHandler',function(done){
    it('should get hello',function(){
        demo.handler('h1',function($scope){
            $scope.res.writeHead(200,{'Content-Type':'text/plain'});
            $scope.res.write('hello,world');
            $scope.res.end();
        });
        demo.get('/test','h1');
        request(demo.server())
        .get('/test')
        .expect(200)
        .end(function(err,res){
            if(err) throw err;
            res.text.should.equal('hello,world');
        });
    });
    it('should cover the previous one',function(done){
        demo.handler('h1',function($scope){
            $scope.res.writeHead(200,{'Content-Type':'text/plain'});
            $scope.res.write('hi,world');
            $scope.res.end();
        });
        request(demo.server(true)).
            get('/test').
            expect(200).
            end(function(err,res){
                if(err) done(err);
                res.text.should.equal('hi,world');
                done();
            });
    });
});

测试效果截图如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值