前言
因为最近使用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测试。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]