使用 tdd/bdd 在 Rails 中测试 mongoid 模型的最佳方法是什么?
到目前为止,为了测试 Rails/mongoid DB 模型,我曾经有一个只有很少数据的真实模型(3 .. 5),试图真实地涵盖每种情况。
我已经在使用 Cucumber 在 BDD 中进行外部测试,但我通常会提前停止,很少使用 rspec,并且没有夹具和模型,担心真实情况并通过 Rails.logger.info 使用 Rails 记录器打印字段值>
但现在我受够了,当模型变得更加复杂时,它就太令人难以承受了。
使用 tdd/bdd 在 Rails 中测试 mongoid 模型的最佳方法是什么? 我可以从哪里开始?
To test rails/mongoid DB models until now, I used to have a real model with few data (3 .. 5), trying to cover every case for real.
I'm already testing outside in BDD with cucumber, but I usually stop earlier, using rspec very little and without fixture and mockup, prefearing real cases and printing the fields values with rails logger via Rails.logger.info
But now I fed up with that, when the models get more complicated, it's too overwhelming.
What's the best way to test mongoid models in rails with tdd/bdd ?
Where can I start ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前没有使用过 Mongoid,但是有什么原因可以解释为什么测试这些模型会与使用 ActiveRecord 进行测试有所不同吗?我个人依赖 RSpec 进行单元测试,它应该只测试模型的外部 API(行为),而不应该过多依赖固定装置或测试内部数据或私有方法。您可以使用模拟 &在很多情况下也可以使用存根来避免数据库持久化。
如果您的模型变得太复杂,您可能需要考虑采用更多 OOP 方法并将一些逻辑提取到不依赖于 Mongoid 的其他类。在这种情况下,您有很多选择,如果满足以下条件,我强烈建议您重构 Ruby你有兴趣了解更多。
I haven't worked with Mongoid before, but is there any reason why testing those models would be any different than one using ActiveRecord? I personally rely on RSpec for unit testing, which should only be testing the external API (behavior) of your models and should not rely much on fixtures or testing internal data or private methods. You can use mocks & stubs to avoid DB persistence in a lot of cases too.
If your models are getting too complicated, you might want to consider taking a more OOP approach and extracting some logic to other classes which do not rely on Mongoid. You have a lot of options in this case and I highly recommend Refactoring Ruby if you're interested in learning more.