在 Rails 中测试模型的最佳实践是什么?

发布于 2024-11-04 11:16:25 字数 557 浏览 1 评论 0原文

应该在 Rails 中测试多少或多少模型?由于该框架基本上为您做了很多事情,我想知道是否值得测试生成的 activerecord 方法以确保它们有效。

你们通过控制器隐式测试它们吗?

在 Java 世界中,如果我使用 Hibernate,我必须编写大量 ORM 映射内容,测试每个实体的保存/删除/查找非常重要 - 即使我只是从基类继承这些方法 - 因为它很容易实现获取错误的地图信息,或者犯一个愚蠢的错误。删除对于测试非常重要,以确保 Hibernate 能够正确级联。

但是,由于您根本没有在 Rails 中配置任何这些东西......它就这么简单......它值得测试,还是您只是假设它有效?您是否只是将自己限制在确保协会按预期运作?

最佳实践是什么?有什么好的测试例子吗?我在这里找到了一个页面: http://guides.rubyonrails.org/testing.html

但它没有具体谈论活动记录。它更多地关注控制器和其他东西。

谢谢!

How much or how little should one test models in rails? Since the framework is basically doing so much for you, I'm wondering if it's worth testing the generated activerecord methods to make sure they work or not.

Do you guys test them implicitly through the controllers?

In the Java world, if I used Hibernate, I had to write so much ORM mapping stuff that testing save/delete/find for each entity was VERY important - even if I just inherited these methods from a base class - because it was easily possible to get the mapping information wrong, or make a silly mistake. Deletes were important to test to make sure Hibernate would cascade properly.

But since you don't configure any of this stuff in rails at all... it's just so simple... is it worth testing, or do you just assume it works? Do you just limit yourself to maybe making sure associations work as expected?

What is the best practice? Any good testing examples? I found a page here:
http://guides.rubyonrails.org/testing.html

But it didn't talk specifically about ActiveRecord. It focused more on controllers and other things.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-11-11 11:16:26

我不会尝试测试活动记录等引入的任何方法。它们已经经过 Rails 团队的测试。他们已经编写了测试用例。

但我会测试我的所有方法以及基本上我在模型中编写的任何自定义代码。首先将它们作为单独的单元进行测试,我将使用单元测试。为了确保整个事情正常工作(模型控制器等),您可以使用集成测试。

Railsguides 对您来说将是一个非常好的起点。之后你可以挖掘更有趣的东西。我建议你看看 rspec 和黄瓜。

其他一些对您有帮助的 stackoverflow 问题(和答案)

I don't try to test any methods introduced by active record and such. They are already tested by the rails team. And they already have written test cases.

but i would test all my methods and basically any custom code that i write in my models. First to test them as individual units i will use unit tests. And to make sure the whole thing works (models controllers..etc) you can use integration tests.

the railsguides would be a very good starting point for you. afterwards you can digg in to more interesting stuff. i would recommend you to take a look and rspec and cucumber.

Some more stackoverflow questions (and answers) that would be helpful to you

尴尬癌患者 2024-11-11 11:16:26

不,测试活动记录不是一个好主意,它已经经过了非常广泛的测试,所以你只会在那里浪费时间。

你应该测试你写的任何内容。您编写的任何模型方法都应该经过测试。也就是说,不要测试 Active Record 删除方法是否有效,而是测试该过程的实现是否有效(例如,如果用户单击“删除”并且他拥有该项目,则该项目将被删除)。测试行为,而不是做事的手段。

看看这个使用 rspec 测试控制器的非常好的演员:

http://railscasts.com/episodes/ 71-testing-controllers-with-rspec

您提到的资源可能也是最好的:)如果您对 rspec 感兴趣,您会在这里找到一些非常好的信息:

http://rspec.info/

No, it's not a good idea to test active record, it's already very extensively tested, so you would only waste your time there.

You should test whatever YOU write. Any model method that you code should be tested. That is, do not test whether the Active Record delete method works, but if your implementation of the process works (like if a user clicks delete and he has the item, it gets deleted). Test the behaviour, not the means of doing it.

Take a look at this very nice cast on testing controllers with rspec :

http://railscasts.com/episodes/71-testing-controllers-with-rspec

Your mentioned resource is probably the best as well :) If you are interested in rspec, you will find some very nice information here :

http://rspec.info/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文