【问题标题】:How to test if else condition in rspec rails如何在 rspec rails 中测试 if else 条件
【发布时间】:2021-01-06 11:29:46
【问题描述】:

我是 RSpec 的新手,我想测试控制器的 if-else 条件,我在互联网上搜索但无法正确找到结果。

  def edit
    if @question.user_id == current_user.id
      render :edit
    else
      redirect_to root_path
    end
  end

我正在使用 Rails 6 请指导我如何在示例的帮助下为这种情况编写 RSpec。

【问题讨论】:

标签: ruby-on-rails rspec-rails ruby-on-rails-6


【解决方案1】:

要在 RSpec 中测试这一点,您需要操纵传递给控制器​​的参数以在条件@question.user_id == current_user.id 上生成变量。

通常在 rspec 中,您会对所有通常包含在前操作中的控制器都有类似的内容

before do
 get :edit, params
end

您传入的params 应根据您要测试的条件而有所不同。

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2019-04-25
    • 1970-01-01
    • 2020-07-22
    • 2019-12-28
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多