Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Social Features in Ruby on Rails Adding State Test Factories

Lisa Rossiter
Lisa Rossiter
3,630 Points

Getting error

I can't figure this one out...

# Running tests:

.........EEEE.........

Finished tests in 0.440641s, 49.9273 tests/s, 65.8132 assertions/s.

  1) Error:
UserFriendshipsControllerTest#test_: #index when logged in should assign user_friendships. :
ActionView::Template::Error: undefined method `full_name' for nil:NilClass
    app/views/user_friendships/index.html.erb:12:in `block in _app_views_user_friendships_index_html_erb__2562809195414119847_70281940281000'
    app/views/user_friendships/index.html.erb:5:in `each'
    app/views/user_friendships/index.html.erb:5:in `_app_views_user_friendships_index_html_erb__2562809195414119847_70281940281000'
    test/controllers/user_friendships_controller_test.rb:27:in `block (3 levels) in <class:UserFriendshipsControllerTest>'

index.html.erb

<div class="page-header">
  <h1>Friends</h1>
</div>

<% @user_friendships.each do |friendship| %>
  <% friend = friendship.friend %>
  <div id="<%= dom_id(friendship) %>" class="friend row">
    <div class="span1">

    </div>
    <div class="span7">
      <strong><%= friend.full_name %></strong><br />
        <% if friendship.pending? %>
          <em>Friendship is pending.</em> <%= link_to "Delete request", edit_user_friendship_path(friendship) %>.
        <% end %>
        <% if friendship.requested? %>
          <em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
        <% end %>
        <% if friendship.accepted? %>
          <em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
        <% end %>
    </div>
  </div>
<% end %>

user_friendships_controller_test.rb

should "display friend's names" do
        assert_match /Pending/, response.body
        assert_match /Active/, response.body
      end

      should "display pending information on a pending friendship" do
        assert_select "#user_friendship_#{@friendship1.id}" do
          assert_select "em", "Friendship is pending."
        end
      end

If you guys need anything else just comment.

Lisa Rossiter
Lisa Rossiter
3,630 Points

Also I seem to be able on my server to view pending requests but no accepted requests

7 Answers

Instead of <% friend.full_name %> can you type in <% friendship.friend.full_name %> and see what happens? Also, you can put <%= debug friendship.friend %> to see what's going on.

Lisa Rossiter
Lisa Rossiter
3,630 Points

The error is not server side its testing side?

Lisa Rossiter
Lisa Rossiter
3,630 Points

I fixed it by using a different fixture user. The one I switched to had an encrypted password. Thanks for your help though buddy.

Check your User model. You should be having full_name method implemented. It should return "first_name" + "last_name"

Lisa Rossiter
Lisa Rossiter
3,630 Points

I do have that method implemented. I suspect its not actually receiving the correct data, if you look everything else is in if statements so full_name is the only method being called.

Andrzej Mega
Andrzej Mega
1,184 Points

Hey Lisa, can you please paste your user fixture here ? I'm having the same problem :) I'm on latest RoR

Thanks !

Lisa Rossiter
Lisa Rossiter
3,630 Points

So in the setup I was using john from my fixtures but it solved my problem when I switched to sam

setup do
        @friendship1 = create(:pending_user_friendship, user: users(:sam), friend: create(:user, first_name: 'Pending', last_name: 'Friend'))
        @friendship2 = create(:accepted_user_friendship, user: users(:sam), friend: create(:user, first_name: 'Active', last_name: 'Friend'))

        sign_in users(:sam)
        get :index
      end

My fixtures(I realise now I should of stuck with the original names to avoid confusion)

sam:
  first_name: "sam"
  last_name: "toms"
  profile_name: "tomzie"
  email: "modernoozedesign3@gmail.com"
  encrypted_password: $2a$10$Fh/Hm8RxDsuPTeBUr6864.GARlcmt7zF2WspaXGr3BIv2C27N.osq
john:
  first_name: "sam"
  last_name: "toms"
  profile_name: "sup"
  email: "modernoozedesign2@gmail.com"
  encrypted_password: "password1"
bob:
  first_name: "Bob"
  last_name: "Barker"
  profile_name: "bobb"
  email: "bob.barker@example.com"
  encrypted_password: "password1"

Let me know if this helps bud :)

Andrzej Mega
Andrzej Mega
1,184 Points

thanks for the quick response and your code :)

But it turns out I actullay have a different problem. My fixtures file was fine.

For some reason I get an error : undefined method `full_name' for nil:NilClass

This only happens when I test the controller. I am runnung latest ruby and latest rails. Does anyone have a clue ?

Lisa Rossiter
Lisa Rossiter
3,630 Points

Post up any relevant code and I will take a look.

Andrzej Mega
Andrzej Mega
1,184 Points

[FIXED]

My issue was caused by my fixtures not including the user ID. which is a little weird since the field should be populated every time a new user is inserted, right ? Even into a TEST db since the schema is already setup. So I must be missing somehting..

Here's WORKING my fixture (thanks @Lisa Rossiter ):

  sam:
  id: "1"
  first_name: "Sam"
  last_name: "Samuel"
  user_name: "ssamuel"
  email: "modernoozedesign3@gmail.com"
  encrypted_password: $2a$10$Fh/Hm8RxDsuPTeBUr6864.GARlcmt7zF2WspaXGr3BIv2C27N.osq

john:
  id: "2"
  first_name: "John"
  last_name: "Jones"
  user_name: "jjones"
  email: "modernoozedesign2@gmail.com"
  encrypted_password: $2a$10$Fh/Hm8RxDsuPTeBUr6864.GARlcmt7zF2WspaXGr3BIv2C27N.osq

bob:
  id: "3"
  first_name: "Bob"
  last_name: "Barker"
  user_name: "bbarker"
  email: "bob.barker@example.com"
  encrypted_password: $2a$10$Fh/Hm8RxDsuPTeBUr6864.GARlcmt7zF2WspaXGr3BIv2C27N.osq
Lisa Rossiter
Lisa Rossiter
3,630 Points

Thanks although I think I know the real problem as what you have done is a fix but not correct could you post your user_friendship_controller_test.rb

Andrzej Mega
Andrzej Mega
1,184 Points

I'm actually not as far with the code yet ! :)

I am still on the first "treebook app" module on the "Testing the Whole App" section.

I decided to develop on latest and greatest ruby version so I am seeing lots of version issues which I'm fixing as I go.

This should probably be it's own topic. Sorry for hijacking your post ! :P

Lisa Rossiter
Lisa Rossiter
3,630 Points

I have also developed nearly the whole lot in the latest versions so if you get stuck give me a mention or post a question on here stuff the system :)