我想要一個簡單的方法來測試我的驗證。我的測試重點是集成,而不是驗證,我不使用TDD。話雖這麼說,我想替換:Rspec中測試驗證的簡短方法
# Estate model specs
describe "#name" do
it "is required" do
estate.name = nil
estate.valid?
expect(estate.errors).to have_key(:name)
end
end
describe "#company" do
it "is required" do
estate.company = nil
estate.valid?
expect(estate.errors).to have_key(:company)
end
end
# and so on..
一些像:
# Estate model specs
requiredFields = [:name, :company, :price, :region, :regions, :typologies, :image]
requiredFields.each do |requiredField|
describe "##{requiredField}" do
it "is required" do
estate[requiredField] = nil
estate.valid?
expect(estate.errors).to have_key(requiredField)
end
end
end
的字段名稱和價格的作品,問題是在協會:
Estate
is an instance of Estate
validations
with required fields
should be valid
#name
is required
#company
is required (FAILED - 1)
#price
is required
#region
is required (FAILED - 2)
...
我認爲,問題是estate[requiredField]
。如果我更改爲company_id將工作。我如何在foreach中做類似estate.requiredField
的事情?