I'm using rails 5 and minitest. I was wondering, how do I test the value of a field in an instance variable of my controller method. I realize that if I want to test if the variable is defined, I can do
assert_not_nil assigns(:issue)
but I'm less clear if I want to test the value of @issue.stop_id . My controller method is
# 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'm trying this in my test 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'm getting a NoSuchMethodError on the line
assert_equal test_stop_id, @issue.stop_id
in the test 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
This may not be the best way to handle it, but I have noticed that using assigns(:instance_var_name)
will return the value of the instance variable, so you could test value from that with something like this:
issue_var = assigns(:issue)
assert_equal test_stop_id, issue_var.stop_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With