How to check attribute value in controller instance variable?

I use rails 5 and minitest. I was wondering how to check the value of a field in an instance variable of my controller method. I understand that if I want to check if a variable is defined, I can do

assert_not_nil assigns(:issue) 

but I'm less clear if I want to check the value of @ issue.stop_id. My management method

  # GET /issues/new def new unless user_signed_in? redirect_to new_user_session_path end @issue = Issue.new(stop_onestop_id: params[:stop_id], line_onestop_id: params[:line_id]) end 

I try this in my testing method

  test "get index page" do get index_url assert_not_nil assigns(:issue) assert_equal test_stop_id, @issue.stop.id assert_equal test_line_id, @issue.line.id assert_response :success end 

but i get a NoSuchMethodError in line

 assert_equal test_stop_id, @issue.stop_id 

in the testing method

 test "logged in should get issues page" do sign_in users(:one) test_stop_id = 1 test_line_id = 1 get new_issue_url, params: {stop_id: test_stop_id, line_id: test_line_id} assert_equal test_stop_id, @issue.stop_id assert_equal test_line_id, @issue.line_id assert_response :success end 
+5
source share
1 answer

This may not be the best way to handle this, but I noticed that using assigns(:instance_var_name) will return the value of the instance variable, so you can check the value with something like this:

 issue_var = assigns(:issue) assert_equal test_stop_id, issue_var.stop_id 
0
source

Source: https://habr.com/ru/post/1274364/


All Articles