2017-02-04 28 views
0

我正在使用node/express/mongo並嘗試運行測試來查找具有特定ID的帖子。ECONNREFUSED:連接被拒絕。爲什麼?所有其他測試都通過

這是我的路線:

app.get('/api/posts/:id', PostController.getPost); 

這是控制器:

getPost(req, res, next) { 
    const postId = req.params.id; 

    Post.findById({ _id: postId }) 
     .then(user => res.send(user)) 
     .catch(next); 
    }, 

這是測試:

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(); 
     }); 
    }); 
    }); 

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(); 
     }); 
    }); 
}); 

});

請希望得到一些幫助,請!

回答

0

檢查GET請求,你使用:UD但你PARAMS是req.params.id

+0

對不起,不,這不是:(我複製並粘貼錯誤的代碼。 – bloppit

0

固定,錯過了/在:.get( API /職位/ $ {} post._id )

相關問題