指定简单模型方法的方法
现在我有一个像这样的模型函数:
Class Address
def first_line
"#{self.building_name} #{self.street_name} #{self.suburb}".squeeze(" ").strip
end
end
我的地址工厂是这样定义的:
Factory.define :address do |f|
f.building_name "Alpha"
f.street_name "Bravo St"
f.suburb "Charlie"
end
基本上有两种编写规范的方法,第一种是:
before(:each) do
@address = Factory.build(:address)
end
@address.first_line.should == @address.building_name + " " +
@address.street_name + " " +
@address.suburb
另一种方法是
@address.first_line.should == "Alpha Bravo St Charlie"
您认为哪种方法更好? 支持一方反对另一方的原因是什么?
Right now I have a model function that is something like this:
Class Address
def first_line
"#{self.building_name} #{self.street_name} #{self.suburb}".squeeze(" ").strip
end
end
My address factory is define like this:
Factory.define :address do |f|
f.building_name "Alpha"
f.street_name "Bravo St"
f.suburb "Charlie"
end
There are basically two ways to write the spec, first one is:
before(:each) do
@address = Factory.build(:address)
end
@address.first_line.should == @address.building_name + " " +
@address.street_name + " " +
@address.suburb
And other way is
@address.first_line.should == "Alpha Bravo St Charlie"
Which way do you think is better? And what is the reason for supporting one against the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两者都不。 第一个有点冗长,而且感觉与该方法的实现有点过于友好。 第二个将你的规格与你的工厂紧密结合在一起,当你需要改变工厂时,这将是一个巨大的痛苦。
相反,使用短秒样式,但在规范中明确使用的数据:
这也很好,因为您可以向该方法提供不同的测试数据,这是测试其其他一些功能所必需的:
Neither. The first is a bit verbose and also feels a bit too pally with the implementation of the method. The second tightly couples your specs to your factories and will be a massive pain when you need to change the Factory.
Instead, use the short second style, but make the data used explicit in the spec:
This is also good because you can provide different test data to the method, which is necessary to test some of its other features: