【发布时间】:2012-02-27 21:42:02
【问题描述】:
我们正在使用 RSpec 2 进行 rails 3.2.1 项目。我的问题是,我是否应该测试每个 activerecord 模型的基本持久性?我曾经在 C#/NHibernate 时代这样做,以确保存在正确的表/映射。
所以如果我有一个客户的姓名、地址和电话字段,我可能会这样写一个 rspec:
describe Customer do
it "saves & retrieves its fields to and from the db"
c = Customer.new
c.name = "Bob Smith"
c.address = "123 some street"
c.phone = "555-555-5555"
or = Order.new
c.orders << or
c.save
found = Customer.find(c.id)
found.should_not be(c)
found.name.should == c.name
found.address.should == c.address
found.phone.should == c.phone
found.orders.count.should == 1
found.orders[0].id.should == or.id
end
end
这是“最佳实践”还是在 ruby/rails/rspec 世界中很常见?我还应该注意,重点不是测试 rails 本身在做什么,而是测试在数据库和模型中设置了正确的字段和关系。
【问题讨论】:
标签: ruby-on-rails unit-testing rspec tdd