Rails模型测试-模拟与工厂 [英] Rails Model Testing - Mocking vs. Factories

查看:69
本文介绍了Rails模型测试-模拟与工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails测试模拟对象与使用工厂对象的最佳实践是什么?当模型可能会传递给外部资源时,是否应该仅使用模拟 ?还是只在测试实际模型并为其他所有模型使用模拟时才使用工厂.

What's the best practice for Rails testing for mocking objects vs. using factory objects. Should mocking only be used when a model might go to an external source? Or do you only use factories when you're testing the actual model and using mocking for everything else.

例如,如果我们有一个包含客户和订单的销售系统,当我们测试客户模型时,我们是否在模拟订单或仅使用工厂订单?它甚至有所作为吗?

For example if we have a sales system with customers and orders when we test the customer models do we mock the order or do we just use a factory order? Does it even make a difference?

推荐答案

我们经常在我们的网上商店中进行过这种辩论,但没有给出明确的答案.工厂的好处是可以测试与数据库的交互,并且我们发现了某些类似的问题,而模拟与分析可能会忽略这些问题.存根.另一方面,此类问题很少见,并且模拟/存根的运行速度要快得多,这鼓励进行更多测试.

We've had this debate often at our web shop, and have not come to a definitive answer. Factories have the benefit of testing the interaction with the database, and we have found some problems that way that would have been missed with mocks & stubs. On the other hand, such problems are rare, and the mocks/stubs run quite a bit faster, which encourages more testing.

因此,我们已经朝着使用模拟&单元测试中的存根而不是工厂,再加上放弃了两者的集成测试,从而测试与模拟&的数据库的功能交互.存根单元测试则不行.这似乎为我们找到了适当的平衡.

So we have evolved toward using mocks & stubs rather than factories in unit tests, combined with integration testing that forgoes both, and so tests functional interactions with the db that mocked & stubbed unit tests do not. This seems to strike the right balance for us.

这篇关于Rails模型测试-模拟与工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆