【问题标题】:Writing rspec for controllers with params?为带有参数的控制器编写 rspec?
【发布时间】:2015-04-22 09:36:08
【问题描述】:

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

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

 end

end

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

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

【问题讨论】:

  • 您能添加您当前尝试的代码吗?
  • sry,我不能在这里发布代码。另外,我认为这在这里无关紧要。我只需要知道如何在 rspec 中提及 if params["c"]params["e"]

标签: ruby rspec params controllers


【解决方案1】:

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

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

希望它能给你指明正确的方向。

祝你好运!

【讨论】:

  • 非常感谢。这真的让我朝着正确的方向前进
猜你喜欢
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
相关资源
最近更新 更多