<p>I'm learning how to do some <em>integration</em> tests with mgo using a temporary mock database following the example from this <a href="https://medium.com/@mvmaasakkers/writing-integration-tests-with-mongodb-support-231580a566cd" rel="nofollow">article</a> however I keep getting "panic: timeout waiting for mongod process to die" caused by calling Stop() method on dbtest.DBServer. As a result after running the test I'm left with an idle mongod process running in the background. Is there something I'm missing?</p>
<hr/>**评论:**<br/><br/>comrade_donkey: <pre><p>So a couple of things.</p>
<ul>
<li>Don't trust an article that doesn't even get the name of the programming language right. It's "Go", not "GO" nor "GOlang" or "Golang".</li>
<li>You don't unit-test your database, you just don't. You unit-test your functional units.</li>
<li>Your tests should be self-contained. You can't expect to have someone running mongodb on their machine just to run the tests.</li>
<li>Mock out the database by writing a FAKE database session, not a "real mock database", that's not a thing. Testable code depends on interfaces, mock those interfaces.</li>
</ul>
<p>PS: did you notice the article is about <strong>integration</strong> tests and not unit tests?</p></pre>emelent: <pre><p>Hmmm, then what's the point of dbtest if not to make these temporary db's? Also, wouldn't mocking the whole mgo.Session be overkill? I mean wouldn't that mean I'd have to mock all the methods I use on the session, either that or check if it's a real dbsession or mock dbsession and act accordingly?</p></pre>comrade_donkey: <pre><blockquote>
<p>what's the point of dbtest</p>
</blockquote>
<p>It's an integration test for the <code>mgo</code> driver itself.</p>
<blockquote>
<p>wouldn't mocking the whole mgo.Session be overkill?</p>
</blockquote>
<p>Probably. It's also difficult to get every edge case exactly identical. Thankfully, you don't need to mock the entire session. You just need to mock the interfaces your code depends on. In most cases, that just create, read, update & delete. If that's not the case, then the first step would be to read about separation of concerns, information hiding, command/query segregation, etc. It's not easy to write testable code.</p></pre>emelent: <pre><p>I guess I'll read more on this, thanks for your input.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传