是否有适当的方法来编辑由: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
我有一个非常大的有效负载,称为animal_payload。我只想更改字段animal_type并在其上发帖。但是,当我这样称呼时::land_animal_payload["data"]["animal_type"] = {data:[{"id":1, "type":land}]}
我得到:
`undefined method `[]' for nil:NilClass
我如何使用相同的有效载荷,但稍微更改一个字段,以便可以调用它?
您要做的是在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