How to claim that an action returns the correct text?

Is there a standard or better way to verify that an action in rails returns the correct text? For example, I have a simple action that is used to perform an ajax check that does the following:

def get_object_id
  if params[:id].blank?
    return render(:text => -1, :layout => false)
  end

  my_object = MyObject.find_by_id(params[:id].strip)

  # If the object is valid return the object id
  if my_object and my_object.active?
    return render(:text => my_object.id, :layout => false)
  end

  # Otherwise, return -1
  return render(:text => -1, :layout => false)
end

Right now I am using the @response object in my tests ... ie:

def test_get_object_id_returns_invalid_for_non_existent_id
  # Act
  get :get_object_id, :id => 999

  # Assert
  assert_response :success
  assert_equal "-1", @response.body
end

But, although I'm new to rails, what I have seen so far seems to suggest that there is probably a better way to do this, besides checking @request.body.

So, my question is for you experts rubies: is this the best way?

+3
source share
1 answer

I think your place is already there.

assert_select XML-, . , , ActionController::TestCase, .

.

def assert_response_equals(v)
  assert_equal v.to_s, @response.body, "Expected response body to be #{v.inspect}"
end

.

+3

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


All Articles