blo*_*pit 0 javascript mocha.js mongodb node.js express
我正在使用 node/express/mongo 并尝试运行测试来查找具有特定 id 的帖子。
这是我的路线:
app.get('/api/posts/:id', PostController.getPost);
Run Code Online (Sandbox Code Playgroud)
这是控制器:
getPost(req, res, next) {
const postId = req.params.id;
Post.findById({ _id: postId })
.then(user => res.send(user))
.catch(next);
},
Run Code Online (Sandbox Code Playgroud)
这是测试:
describe('Post Controller', () => {
it('find a post with a particular id', (done) => {
const post = new Post({
text: 'This is a post',
voteCount: 0,
commentCount: 0,
createdAt: 0,
expiresAt: 0
});
post.save().then(() => {
console.log(post._id);
request(app)
.get(`api/posts/${post._id}`)
.set('Accept', 'application/json')
.expect(200)
.end((err, res) => {
console.log(res);
if (err) return done(err);
//assert(response.body.obj.firstName === 'Matt');
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
控制台post._id记录得很好。
响应只是记录为null.
return done(err)是回来的Error: ECONNREFUSED: Connection refused
我知道这条路线正在运行,因为它在 上运行得很好postman。知道为什么它可能无法通过测试吗?
我的所有其他测试都运行得很好,例如这个:
it('POST to /api/posts creates a new post', done => {
Post.count().then(count => {
request(app)
.post('/api/posts')
.send({
text: 'This is a post',
voteCount: 0,
commentCount: 0,
createdAt: 0,
expiresAt: 0
})
.end(() => {
Post.count().then(newCount => {
assert(count + 1 === newCount);
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
});
请提供一些帮助,不胜感激!
| 归档时间: |
|
| 查看次数: |
1602 次 |
| 最近记录: |