Skip to content

Commit 8f56cf8

Browse files
SF-Zhoudead-horse
authored andcommitted
fix: add mock in cnode api unittest (#21)
1 parent 5d02ab5 commit 8f56cf8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cnode-api/config/config.default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
errorHandler: {
66
match: '/api',
77
},
8+
keys: 'my super cool keys',
89
};

cnode-api/test/app/controller/topics.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('test/app/controller/topics.test.js', () => {
1414
afterEach(mock.restore);
1515

1616
it('should GET /api/v2/topics', function* () {
17+
app.mockService('topics', 'list', [{
18+
content: 'Mock List',
19+
}]);
1720
const r = yield request(app.callback())
1821
.get('/api/v2/topics')
1922
.expect(200);
@@ -33,13 +36,19 @@ describe('test/app/controller/topics.test.js', () => {
3336
});
3437

3538
it('should GET /api/v2/topics/:id 404', function* () {
39+
const err = new Error('not found error');
40+
err.status = 404;
41+
app.mockService('topics', 'show', err);
3642
yield request(app.callback())
3743
.get('/api/v2/topics/5433d5e4e737cbe96dcef300')
3844
.expect(404);
3945
});
4046

4147
it('should POST /api/v2/topics/ 422', function* () {
4248
app.mockCsrf();
49+
const err = new Error('validation failed');
50+
err.status = 422;
51+
app.mockService('topics', 'create', err);
4352
yield request(app.callback())
4453
.post('/api/v2/topics')
4554
.send({

cnode-api/test/app/service/topics.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ describe('test/app/service/topics.test.js', () => {
1616

1717
describe('show()', () => {
1818
it('should with render success', function* () {
19+
app.mockHttpclient(`${ctx.service.topics.root}/topic/57ea257b3670ca3f44c5beb6`, 'GET', {
20+
data: {
21+
success: true,
22+
data: {
23+
content: '<div class="markdown-text">Super Mock Content</div>',
24+
replies: [],
25+
},
26+
},
27+
});
1928
const topic = yield ctx.service.topics.show({
2029
id: '57ea257b3670ca3f44c5beb6',
2130
mdrender: true,
@@ -27,6 +36,15 @@ describe('test/app/service/topics.test.js', () => {
2736
});
2837

2938
it('should without render success', function* () {
39+
app.mockHttpclient(`${ctx.service.topics.root}/topic/57ea257b3670ca3f44c5beb6`, 'GET', {
40+
data: {
41+
success: true,
42+
data: {
43+
content: 'Super Mock Content',
44+
replies: [],
45+
},
46+
},
47+
});
3048
const topic = yield ctx.service.topics.show({
3149
id: '57ea257b3670ca3f44c5beb6',
3250
mdrender: false,
@@ -38,6 +56,13 @@ describe('test/app/service/topics.test.js', () => {
3856
});
3957

4058
it('should response 404 when topic id not exist', function* () {
59+
app.mockHttpclient(`${ctx.service.topics.root}/topic/5433d5e4e737cbe96dcef300`, 'GET', {
60+
status: 404,
61+
data: {
62+
error_msg: '话题不存在',
63+
},
64+
});
65+
4166
try {
4267
yield ctx.service.topics.show({
4368
id: '5433d5e4e737cbe96dcef300',
@@ -53,6 +78,18 @@ describe('test/app/service/topics.test.js', () => {
5378

5479
describe('list()', () => {
5580
it('should with render, limit and tab success', function* () {
81+
app.mockHttpclient(`${ctx.service.topics.root}/topics`, 'GET', {
82+
data: {
83+
success: true,
84+
data: [
85+
{
86+
content: '<div class="markdown-text">mock content</div>',
87+
},
88+
{}, {}, {}, {},
89+
],
90+
},
91+
});
92+
5693
const topics = yield ctx.service.topics.list({
5794
mdrender: true,
5895
limit: 5,
@@ -67,6 +104,13 @@ describe('test/app/service/topics.test.js', () => {
67104

68105
describe('create()', () => {
69106
it('should create failed by accesstoken error', function* () {
107+
app.mockHttpclient(`${ctx.service.topics.root}/topics`, 'POST', {
108+
status: 401,
109+
data: {
110+
error_msg: '错误的accessToken',
111+
},
112+
});
113+
70114
try {
71115
yield ctx.service.topics.create({
72116
accesstoken: 'hello',
@@ -97,6 +141,13 @@ describe('test/app/service/topics.test.js', () => {
97141

98142
describe('update()', () => {
99143
it('should update failed by accesstoken error', function* () {
144+
app.mockHttpclient(`${ctx.service.topics.root}/topics/update`, 'POST', {
145+
status: 401,
146+
data: {
147+
error_msg: '错误的accessToken',
148+
},
149+
});
150+
100151
try {
101152
yield ctx.service.topics.update({
102153
id: '57ea257b3670ca3f44c5beb6',

0 commit comments

Comments
 (0)