Rails 3:如何测试使用acts_as风格插件的模型?
我编写了一个非常简单的插件(遵循 Rails 指南),它添加了 has_sex
方法到 ActiveRecord::Base
,调用时会向模型添加一些实例方法(用于获取适当的代词等),并验证:性别、包含: {in:%w(男女)}
。
我应该如何在利用它的模型上编写单元测试?
我可以测试 Person < HasSex::InstanceMethods
,如果在 Person 上调用 has_sex
则为 true(并且插件测试确保产生所需的行为结果)。但我想知道这是否真的是在测试实现而不是行为?
另一方面,我可以测试 Person.sex
是否只允许值“male”和“female”,以及各种实例方法是否具有所需的效果,但随后我重复了我的测试为插件本身编写。
这里最好的方法是什么?作为记录,我正在使用 Test::Unit 和 Shoulda 进行测试,但我对原理更感兴趣,而不是更详细的细节。
I've written a very simple plugin (following the Rails guide) that adds a has_sex
method to ActiveRecord::Base
, which, when called, adds a few instance methods (for getting appropriate pronouns and so on) to the model and validates :sex, inclusion: {in: %w(male female)}
.
How should I write unit tests on models that utilise this?
I could test that Person < HasSex::InstanceMethods
, which will be true if has_sex
was called on Person (and the plugin tests ensure that the desired behaviour results). But I wonder if this is really testing implementation and not behaviour?
On the other hand, I could test if Person.sex
only allows values 'male' and 'female', and that the various instance methods have the desired effect, but then I'm duplicating tests that I wrote for the plugin itself.
What's the best approach here? For the record, I'm testing with Test::Unit and Shoulda, but I'm more interested in the principle rather than the finer details.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论