los*_*193 5 rspec ruby-on-rails
是否有正确的方法来编辑 :let 创建的变量并调用它?
describe "#create" do
let(:animal_payload) {
{
"data": {
......
"animal_type": {
"data": {
"id": 1,
"type": "sea",
}
},
}
}
}
let(:land_animal_payload) {animal_payload}
:land_animal_payload["data"]["animal_type"] = {data:[{"id":1, "type":land}]}
context "when animal is type land" do
subject { post :create, params: land_animal_payload }
it "should create a land animal" do
....
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个非常大的有效负载,称为animal_payload。我只想更改 Animal_type 字段并在其上调用帖子。然而,当我调用这个: :land_animal_payload["data"]["animal_type"] = {data:[{"id":1, "type":land}]}我得到:
`undefined method `[]' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
如何使用相同的有效负载,但稍微更改其中一个字段以便我可以调用它?
您想要做的是在 let 变量中使用 let 变量:
describe "#create" do
subject { post :create, params: animal_payload }
let(:animal_payload) {
{
"data": {
......
"animal_type": {
"data": {
"id": 1,
"type": animal_type,
}
},
}
}
}
context 'when sea animal' do
let(:animal_type) { 'sea' }
it "should create a sea animal" do
....
end
end
context 'when land animal' do
let(:animal_type) { 'land' }
it "should create a land animal" do
....
end
end
end
Run Code Online (Sandbox Code Playgroud)