node测试基础

作者: suemi | 来源:发表于2014-12-22 15:02 被阅读54次

前言

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

mocha

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

describe

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

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

it

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

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

describe('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

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

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

expect

var 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为我们提供了这样的功能。

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

要模拟异步ajax怎么办呢

//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').
    ....

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

完整示例

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

var 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();
            });
    });
});

测试效果截图如下


图片描述图片描述

相关文章

  • node测试基础

    前言 到了开发的后期,测试工作往往是重中之重,但是测试本身又十分繁琐与复杂。对于使用js的朋友,对于我接下来要讲的...

  • Vue学习第一天

    基础知识 node 安装 Node(傻瓜式安装) npm基础 npm 之于 Node.js ,就像 pip 之于 ...

  • Rundeck并行执行测试

    目录 job配置 Node First测试 Parallel测试 Sequential测试 并行测试结果 job执...

  • vue-cli2(笔记)

    1、 vue-cli 安装 1)测试是否有node 环境 并且 node 版本>=v 8.11.0使用 node ...

  • 软件测试文章收集

    1.软件测试理论概念 软件测试基础 软件测试基础知识大全(上篇) 软件测试基础知识大全(下篇) 软件测试基础学习 ...

  • 前端Node.js 基础

    一 .Node.js 基础 目录 Node开发概述Node运行环境搭建Node.js快速入门 1. Node开发概...

  • Node.js 进阶途径

    Node.js 源码PR Node.js 性能测试 node-clinic NPM 包推荐get-value尝试访...

  • node.js 常用技术(二) 测试相关

    node.js的测试是非常重要的, 通常要写一个函数就要写出其对应的测试通常, node.js测试 可以使用moc...

  • webpack

    基于node环境,必须确保node已经安装好?node -vnpm -v webpack基础入门官网: http:...

  • HashMap 源码理解

    基础 Node定义 table hash表,Node数组。 size: hash表中Node节点总数,与hash...

网友评论

    本文标题:node测试基础

    本文链接:https://www.haomeiwen.com/subject/gchatttx.html