5

Is there a proper way to edit a variable created by :let and call it?

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

I have a very large payload called animal_payload. I just want to change the field animal_type and call a post on it. However, when I call this: :land_animal_payload["data"]["animal_type"] = {data:[{"id":1, "type":land}]} I get:

`undefined method `[]' for nil:NilClass

How can I use the same payload but change one of the fields slightly so that I can call it?

0

1 Answer 1

11

What you want to do is use a let variable inside the let variable:

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.