Rspec中:let变量中的更新/重新分配值字段

问题描述 投票:0回答:1

是否有适当的方法来编辑由: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

我如何使用相同的有效载荷,但稍微更改一个字段,以便可以调用它?

ruby-on-rails rspec
1个回答
2
投票

您要做的是在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
© www.soinside.com 2019 - 2024. All rights reserved.