这可能与this question完全相反。请考虑以下规范:
describe Record do
it "calculates the first term grade" do
test = stub(id: 1, final_grade: 20)
audition = stub(id: 1, final_grade: 20)
record = Record.create!({
first_term_knowledge: 15,
first_term_attitude: 10,
first_term_test_id: test.id,
first_term_audition_id: audition.id})
record.calculated_grade.should_not be_nil
end
end
Record
模型将所有这些属性迁移到数据库中。
规范失败了,这个解释是:
1) Record calculates the first term grade
Failure/Error: record = Record.create!({
ActiveRecord::UnknownAttributeError:
unknown attribute: first_term_knowledge
# ./spec/models/record_spec.rb:7:in `block (2 levels) in <top (required)>'
Rails文档声明必须设置attr_accessor
才能允许批量分配。我试过了,测试继续进行。然而,这是一个开发峰值,没有事先测试,first_term_knowledge
成功传递。
如果生产代码已经无法使用attr_accessor
,我应该如何进行测试?这是一种错误的测试方法吗?我应该使用模拟器吗?