Skip to content

Commit

Permalink
fix: add unittest for cnode-api (eggjs#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Jan 13, 2017
1 parent 48f41d8 commit 56f103a
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 27 deletions.
13 changes: 6 additions & 7 deletions cnode-api/app/service/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module.exports = app => {
class TopicService extends app.Service {
constructor(ctx) {
super(ctx);
this.root = 'https://cnodejs.org/api/v1/topics';
this.root = 'https://cnodejs.org/api/v1';
}

* show(params) {
const result = yield this.ctx.curl(`${this.root}/${params.id}`, {
const result = yield this.ctx.curl(`${this.root}/topic/${params.id}`, {
data: {
mdrender: params.mdrender,
accesstoken: params.accesstoken,
Expand All @@ -21,7 +21,7 @@ module.exports = app => {
}

* list(params) {
const result = yield this.ctx.curl(this.root, {
const result = yield this.ctx.curl(`${this.root}/topics`, {
data: params,
dataType: 'json',
});
Expand All @@ -31,7 +31,7 @@ module.exports = app => {
}

* create(params) {
const result = yield this.ctx.curl(this.root, {
const result = yield this.ctx.curl(`${this.root}/topics`, {
method: 'post',
data: params,
dataType: 'json',
Expand All @@ -43,7 +43,7 @@ module.exports = app => {
}

* update(params) {
const result = yield this.ctx.curl(`${this.root}/${params.id}`, {
const result = yield this.ctx.curl(`${this.root}/topics/update`, {
method: 'post',
data: params,
dataType: 'json',
Expand All @@ -59,8 +59,7 @@ module.exports = app => {
this.ctx.throw(result.status, errorMsg);
}
if (!result.data.success) {
this.logger.error('remote format error:', result.data);
this.ctx.throw(500, 'remote response error');
this.ctx.throw(500, 'remote response error', { data: result.data });
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions cnode-api/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
'use strict';

module.exports = appInfo => {
const config = {};

// should change to your own
config.keys = appInfo.name + '123456';

config.middleware = [ 'errorHandler' ];

config.errorHandler = {
module.exports = {
middleware: [ 'errorHandler' ],
errorHandler: {
match: '/api',
};

return config;
},
};
4 changes: 2 additions & 2 deletions cnode-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "cnode RESTful API",
"private": true,
"dependencies": {
"egg": "^0.6.3"
"egg": "^0.7.0"
},
"devDependencies": {
"autod": "^2.7.1",
"egg-bin": "^1.7.0",
"egg-ci": "^1.1.0",
"egg-mock": "^1.2.0",
"egg-mock": "^2.0.0",
"eslint": "^3.10.0",
"eslint-config-egg": "^3.2.0",
"supertest": "^2.0.1",
Expand Down
10 changes: 4 additions & 6 deletions cnode-api/test/app/controller/topic.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');
const mock = require('egg-mock');
const assert = require('assert');

describe('test/app/controller/topic.test.js', () => {
let app;
before(() => {
app = mm.app();
app = mock.app();
return app.ready();
});

after(() => app.close());

afterEach(mm.restore);
afterEach(mock.restore);

it('should GET /api/v2/topics', function* () {
const r = yield request(app.callback())
Expand All @@ -36,7 +34,7 @@ describe('test/app/controller/topic.test.js', () => {

it('should GET /api/v2/topics/:id 404', function* () {
yield request(app.callback())
.get('/api/v2/topics/999999999999')
.get('/api/v2/topics/5433d5e4e737cbe96dcef300')
.expect(404);
});

Expand Down
128 changes: 128 additions & 0 deletions cnode-api/test/app/service/topic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
'use strict';

const assert = require('assert');
const mock = require('egg-mock');

describe('test/app/service/topic.test.js', () => {
let app;
let ctx;
before(function* () {
app = mock.app();
yield app.ready();
ctx = app.mockContext();
});

afterEach(mock.restore);

describe('show()', () => {
it('should with render success', function* () {
const topic = yield ctx.service.topic.show({
id: '57ea257b3670ca3f44c5beb6',
mdrender: true,
});
assert(topic);
assert(typeof topic.content === 'string');
assert(topic.content.indexOf('<div class="markdown-text">') >= 0);
assert(Array.isArray(topic.replies));
});

it('should without render success', function* () {
const topic = yield ctx.service.topic.show({
id: '57ea257b3670ca3f44c5beb6',
mdrender: false,
});
assert(topic);
assert(typeof topic.content === 'string');
assert(topic.content.indexOf('<div class="markdown-text">') === -1);
assert(Array.isArray(topic.replies));
});

it('should response 404 when topic id not exist', function* () {
try {
yield ctx.service.topic.show({
id: '5433d5e4e737cbe96dcef300',
mdrender: false,
});
throw new Error('should not excute');
} catch (err) {
assert(err.status === 404);
assert(err.message === '话题不存在');
}
});
});

describe('list()', () => {
it('should with render, limit and tab success', function* () {
const topics = yield ctx.service.topic.list({
mdrender: true,
limit: 5,
tab: 'share',
});
assert(topics);
assert(topics.length === 5);
assert(typeof topics[0].content === 'string');
assert(topics[0].content.indexOf('<div class="markdown-text">') >= 0);
});
});

describe('create()', () => {
it('should create failed by accesstoken error', function* () {
try {
yield ctx.service.topic.create({
accesstoken: 'hello',
title: 'title',
content: 'content',
});
} catch (err) {
assert(err.status === 401);
assert(err.message === '错误的accessToken');
}
});

it('should create success', function* () {
app.mockHttpclient(`${ctx.service.topic.root}/topics`, 'POST', {
data: {
success: true,
topic_id: '5433d5e4e737cbe96dcef312',
},
});
const id = yield ctx.service.topic.create({
accesstoken: 'hello',
title: 'title',
content: 'content',
});
assert(id === '5433d5e4e737cbe96dcef312');
});
});

describe('update()', () => {
it('should update failed by accesstoken error', function* () {
try {
yield ctx.service.topic.update({
id: '57ea257b3670ca3f44c5beb6',
accesstoken: 'hello',
title: 'title',
content: 'content',
});
} catch (err) {
assert(err.status === 401);
assert(err.message === '错误的accessToken');
}
});

it('should update success', function* () {
app.mockHttpclient(`${ctx.service.topic.root}/topics/update`, 'POST', {
data: {
success: true,
topic_id: '5433d5e4e737cbe96dcef312',
},
});
yield ctx.service.topic.update({
id: '57ea257b3670ca3f44c5beb6',
accesstoken: 'hello',
title: 'title',
content: 'content',
});
});
});
});

0 comments on commit 56f103a

Please sign in to comment.