这可能与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
endRecord模型将所有这些属性迁移到数据库中。
该规范失败了,原因如下:
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的情况下工作时,我应该如何让测试工作?这是一种错误的测试方式吗?我是不是应该对此使用mock呢?
发布于 2011-11-17 05:49:35
感谢Brett Bender回答了这个问题,这是一个伟大的举动!
rake db:test:prepare是在测试环境中加载整个模式所必需的。
https://stackoverflow.com/questions/8159036
复制相似问题