Balaji Radhakrishnan
Balaji Radhakrishnan

Reputation: 1050

Writing rspec for controllers with params?

I have a controller which has the following method

class <controllername> < ApplicationController
 def method
  if params["c"]
   .....
  elsif params["e"]
   .....
  else
   .....
  end

 end

end

Now, I want to write rspec for the above code.

How can I write separate context for both the params and how will I mention them as a get method.

Upvotes: 0

Views: 227

Answers (1)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

If I understand your question correctly, you can try approach like this:

RSpec.describe <controllername>, :type => :controller do
  describe "GET my_method" do
    context "param 'c' is provided"
      get :my_method, { "c" => "sample value" }
      expect(response).to have_http_status(:success)
    end

    context "param 'e' is provided"
      get :my_method, { "e" => "sample value" }
      expect(response).to have_http_status(:success)
    end
  end
end

Hope it puts you in proper direction.

Good luck!

Upvotes: 1

Related Questions