Skip to content

run the request in controller test just once but have multiple tests having expectations. possible? #2448

@krtschmr

Description

@krtschmr

i wonder if it's possible to run the request only once and then have multiple expectations on it structured in multiple tests.

let's pretend one request creates 10 different things and we need 20 expectations here (stupid example, i know) and we don't want to have one single test but structure it more.
the alternative would be to have a request in every test but that slows down the pipeline a lot.

any possibility?

context "runs the request just once" do
    before(:all) do
      @document = create :document
      @request = get :show, params: {id: @document.id}
    end
    it "has expectation" do      
    end
    it "has more expectation" do      
    end
    it "has other expectations" do      
    end
    it "checks random stuff" do
    end
  end

if i run this, i get

@routes is nil: make sure you set it in your test's setup method.

one working way is, but is there another?


context "runs the request just once" do
    request = nil
    before(:each) do
      request ||= begin
        puts "run me only once buddy"
        @document = create :document
        get :show, params: {id: @document.id}
      end
    end
    it "has expectation" do
      expect(request).to have_http_status(302)
    end
    it "has more expectation" do
      expect(request).to have_http_status(302)
    end
    it "has other expectations" do
      expect(request).to have_http_status(302)
    end
    it "checks random stuff" do
      expect(request).to have_http_status(302)
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions