Skip to content

Commit

Permalink
test(delegate): stub external request
Browse files Browse the repository at this point in the history
  • Loading branch information
paradite committed Feb 24, 2019
1 parent bf953df commit 7c7788e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
1 change: 0 additions & 1 deletion app/controller/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { Controller } = require('egg');
class DelegateController extends Controller {
async message() {
const ctx = this.ctx;
console.log(ctx.request.body);
const { webhook, text, title } = ctx.request.body;

const res = await ctx.helper.sendMarkdown({
Expand Down
34 changes: 26 additions & 8 deletions test/app/controller/delegate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@
const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/controller/delegate.test.js', () => {
const webhook = {
url: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx',
};
const title = 'test title';
const text = [ 'text1', 'text2' ];
const responseData = { test: 1 };

before(function() {
app.mockContext({
helper: {
sendMarkdown: async options => {
assert.deepStrictEqual(options.webhook, webhook);
assert.deepStrictEqual(options.title, title);
assert.deepStrictEqual(options.text, text);
return {
data: responseData,
};
},
},
});
});

it('POST /api/delegate/message delegate message', async () => {
const { body } = await app
.httpRequest()
.post('/api/delegate/message')
.send({
webhook: {
url: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx',
},
title: 'title',
text: [ 'text1', 'text2' ],
webhook,
title,
text,
});
assert(body.success);
const data = body.data;
// data is the response from DingTalk, something like { errmsg: 'token is not exist', errcode: 300001 }
assert(data);
assert.deepStrictEqual(body.data, responseData);
});
});

0 comments on commit 7c7788e

Please sign in to comment.