Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do test the value of an attribute in a controller instance variable?

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
like image 980
Dave Avatar asked Nov 08 '22 12:11

Dave


1 Answers

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
like image 111
octopushugs Avatar answered Nov 15 '22 11:11

octopushugs