我在 rspec 中有以下测试用例
let(:detail) { FactoryGirl.create(:detail) }
let(:url) do
"/detail/#{detail.detail_id}"
"/user/#{detail.user.id}/details"
end
let(:location_header) do
detail_url(detail_id: detail.detail_id,
user: detail.user.id, location: detail.location)
end
before do
post url, params: params
end
context 'when valid location and value are passed, detail is created successfully.' do
let(:params) {{detail_app_id: detail.detail_app_id, location: detail.location, value: 'value'}}
it { expect(response.status).to eq(201) }
end
我正在学习 ruby rspec,但出现错误" {"error":"Validation failed: location has already been taken"}"
当我在参数中更改位置值时,它会传递,但由于我正在为它创建工厂女孩,我想使用该值而不是传递不同的值。知道为什么我会收到这个错误吗?
你需要进入你的一个工厂(我假设细节)并正确分配它的位置(工厂/detail.rb)。
这里发生的事情是,您正在尝试创建一个新的细节,它试图保存该细节,但它未能通过验证(我假设您在细节模型上有一个唯一的位置验证)
查看 http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md 序列部分,了解如何在需要时提供唯一属性。