DDR爱好者之家 Design By 杰米
                                前言
因为最近使用koa2做项目测试开发,所以想整合下travis ci,网上资料也比较少,于是自己就整了个,做个记录。分享出来供大家参考学习,下面来看看详细的介绍吧。
方法如下:
先来看下travis.yml的配置
language: node_js node_js: - "6" before_script: - ./node_modules/.bin/knex migrate:latest --knexfile='./app/knexfile.js' script: - npm run test
因为是接口测试,所以首先需要做表创建等操作。
测试的命令:
NODE_ENV=production NODE_CONFIG_DIR='./app/config/' ./node_modules/.bin/mocha --require 'babel-polyfill' --compilers js:babel-register ./app/test/**/*.js
主要是测试这里,使用了supertest,大概看下是如何调用的。
const request = require('supertest');
const should = require('should');
const index = require('../../index');
let app = request(index.listen());
describe('/api/persons', function() {
 let personId;
 it('POST /api/persons - create person success and respond with 200', function(done) {
 app.post('/api/persons')
  .send({
  'firstName': 'Jennifer',
  'lastName': 'Lawrence',
  'age': 24
  })
  .expect(200)
  .expect(function(res) {
  (res.body.id > 0).should.be.true;
  })
  .end(function(err, res) {
  if (err) {
   return done(err);
  }
  let resJson = JSON.parse(res.text);
  personId = resJson.id;
  done();
  })
 });
 it('GET /api/persons - fetch persons item', function(done) {
 app.get('/api/persons')
  .expect(200)
  .expect(function(res) {
  (res.body.length > 0).should.be.true;
  })
  .end(function(err, res) {
  if (err) {
   return done(err);
  }
  done();
  })
 });
 it('GET /api/persons/:id - fetch a person', function(done) {
 app.get(`/api/persons/${personId}`)
  .expect(200)
  .expect(function(res) {
  (res.body.id == personId).should.be.true;
  })
  .end(function(err, res) {
  if (err) {
   return done(err);
  }
  done();
  })
 });
 it('DELETE /api/persons/:id - delete a person', function(done) {
 app.delete(`/api/persons/${personId}`)
  .expect(200)
  .end(function(err, res) {
  if (err) {
   return done(err);
  }
  done();
  })
 });
 it('GET /api/persons/:id - fetch a person should 404', function(done) {
 app.get(`/api/persons/${personId}`)
  .expect(404)
  .end(function(err, res) {
  if (err) {
   return done(err);
  }
  done();
  })
 });
});
这里主要注意的是
const index = require('../../index');
需要将koa实例暴漏出来,不然在做travis ci的集成后,启动了项目,测试的时候依然找不到具体访问地址。
来看下我的index.js
import Knex from 'knex';
import {
 Model
} from 'objection';
import knexConfig from './knexfile';
import config from 'config';
import Koa from 'koa';
import koaLogger from 'koa-logger';
import bodyParser from 'koa-bodyparser';
import render from 'koa-ejs';
import co from 'co';
import koaStatic from "koa2-static"
import router from './router';
const path = require('path');
// initial knex
const knex = Knex(knexConfig.development);
Model.knex(knex);
// initial app
const app = new Koa();
// initial render
render(app, {
 root: path.join(__dirname + '/view'),
 layout: 'template',
 viewExt: 'ejs',
 cache: true,
 debug: true
});
app.context.render = co.wrap(app.context.render);
// initial static
app.use(koaLogger())
 .use(bodyParser())
 .use(router.routes())
 .use(koaStatic({
 path: '/web',
 root: __dirname + "/../static"
 }));
module.exports = app;
需要注意的是这里的
module.exports = app;
暴漏出来,再supertest中才可以独立启动server测试。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
DDR爱好者之家 Design By 杰米
                            
                                广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
                        免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
                        暂无评论...
                                    稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年11月04日
                                2025年11月04日
                    - 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
 - 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
 - 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
 - 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
 - 群星《2024好听新歌42》AI调整音效【WAV分轨】
 - 王思雨-《思念陪着鸿雁飞》WAV
 - 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
 - 李健《无时无刻》[WAV+CUE][590M]
 - 陈奕迅《酝酿》[WAV分轨][502M]
 - 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
 - 群星《吉他王(黑胶CD)》[WAV+CUE]
 - 齐秦《穿乐(穿越)》[WAV+CUE]
 - 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
 - 邝美云《邝美云精装歌集》[DSF][1.6G]
 - 吕方《爱一回伤一回》[WAV+CUE][454M]