Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

How to test if else condition in rspec rails

I'm new to RSpec and I want to test the if-else condition of a controller, I searched on the internet but couldn't find results properly.

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

I am using rails 6 Please guide me that how do I write RSpec for this condition, with the help of an example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To test this out in RSpec you need to manipulate the parameters that are pass to the controller to generate the variables on the condition@question.user_id == current_user.id.

usually in rspec you would have something like this to all the controller usually inclosed with a before action

before do
 get :edit, params
end

The params you pass in should vary based on the conditions you want to test.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...