为带有参数的控制器编写 rspec?

Writing rspec for controllers with params?

我有一个具有以下方法的控制器

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

 end

end

现在,我想为上面的代码写rspec。

如何为这两个参数编写单独的上下文以及如何将它们作为 get 方法提及。

如果我理解正确你的问题,你可以尝试这样的方法:

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

希望它能为您指明正确的方向。

祝你好运!