Ryan Murphy
Ryan Murphy

Reputation: 842

Testing view elements

I am trying to test a value that is displayed on a page, the code that displays the value is:

<% @user ||= current_user %>
<div class="stats">
  <a href="<%= following_user_path(@user) %>">
    <strong id="following" class="stat">
      **<%= @user.following.count %>**
    </strong>
    following
  </a>
  <a href="<%= followers_user_path(@user) %>">
    <strong id="followers" class="stat">
      <%= @user.followers.count %>
    </strong>
    followers
  </a>
</div>

I am trying to use assert_equals as such: assert_equals user.followers.count, (The followning value displayed)

But I am not sure how to reference the value that is displayed on the page. Do I need to assign the value an id? If so how would I right the reference to it in the test?

I hope I have provided enough information.

Regards,

Ryan.

Upvotes: 0

Views: 65

Answers (1)

Malik Shahzad
Malik Shahzad

Reputation: 6959

There can be two scenerios (and you should be doing both):

  • You want to verify the count calculated in controller.
  • You want to test the view(HTML)

For part 1: Verify what your controller has, assigns() gives you controller objects/variables.

assert_equals assigns(:user).following.count, 2

For part 2: As describe here, to test your views use assert_select. like:

assert_select 'div.stats a strong#following', assigns(:user).following.count

Also you should replace assigns(:user).following.count with some hardocded number, so you always make sure that some other thing is not changing the following count.

Upvotes: 1

Related Questions