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
Test Factories
Hello Peeps...the test error posted below is a hurdle i cant seem to get over...any assistance would be appreciated. This happens when testing -
should "get the index page without error" do
assert_response :success
end
1) Error:
test: #index when logged in should get the index page without error. (UserFriendshipsControllerTest):
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
/Users/seun/.rvm/gems/ruby-1.9.3- p327/gems/activerecord- 3.2.9/lib/active_record/validations.rb:56:in `save!'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:264:in `block in save!'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:208:in `transaction'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:264:in `save!'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/evaluation.rb:15:in `[]'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/evaluation.rb:15:in `create'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/strategy/create.rb:9:in `tap'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/factory.rb:42:in `run'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.9/lib/active_support/notifications.rb:125:in `instrument'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:22:in `run'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
test/functional/user_friendships_controller_test.rb:16:in `block (3 levels) in <class:UserFriendshipsControllerTest>'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:416:in `call'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:416:in `block in run_current_setup_blocks'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:415:in `each'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:415:in `run_current_setup_blocks'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:397:in `block in create_test_from_should_hash'
/Users/seun/.rvm/gems/ruby-1.9.3-p327/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:28:in `run'
19 tests, 26 assertions, 0 failures, 1 errors, 0 skips
10 Answers
Solved!
Amit Bijlani
Treehouse Guest Teacher@Bernard can you share how you solved the issue? It might be helpful to others if they come across the same problem.
Amit...am not particularly sure how i got around this issue, i cleaned a couple of erroneous syntax and ran rake db:test:prepare.... wish i could be more helpful
Jason Seifer
Treehouse Guest TeacherThe reason that running rake db:test:prepare worked is that it cleared out some information from the test database. The test database may have had an error and had some stale data in it. By clearing it, the offending data was removed.
Yulai Fjeld
680 PointsI am getting a similar problem,
#index when logged in should get the index page without error. (UserFriendshipsControllerTest): ActionView::Template::Error: undefined method 'each' for nil:NilClass
21 tests, 26 assertions, 0 failures, 3 errors, 0 skips
I tried rake:db:test:prepare, but still get the same message. I suspect the problem is with my 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">
<%= link_to image_tag(friend.gravatar_url), profile_path(friend) %>
</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 %>
Jason Seifer
Treehouse Guest TeacherHey Yulai, can you paste the full output of the errors you're getting?
Yulai Fjeld
680 PointsHello, Jason.
Solved the problem, and got the help here: http://teamtreehouse.com/forum/mutual-friendships-cannot-accept-friendship
Thank you.
Doug Wright
8,706 PointsI can't seem to figure out why my friend = friendship.friend code in my index is turning up nil. Can't pass the user_friendships_controller test.
1) Error:
test: #index when logged in should assign user_friendships. (UserFriendshipsControllerTest):
ActionView::Template::Error: undefined methodgravatar_url' for nil:NilClass
/Users/iMac/Documents/DougCode/treebook/app/views/user_friendships/index.html.erb:17:in block in _app_views_user_friendships_index_html_erb__3894510645546886624_70265993576780'
Index.html.erb
<% @user_friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
<%= link_to image_tag(friend.gravatar_url), profile_path(friend) %>
</div>
Jason Seifer
Treehouse Guest TeacherHey Doug Wright can you zip up your code and email it to help@teamtreehouse.com and I'll take a look?
Doug Wright
8,706 PointsHi Jason,
Well, I avoided the failing test by adding a conditional: https://github.com/mrdougwright/treebook/commit/3357ebfac000ea81bcba6cee16f3457b3ec7156c
Now that I've finished the course, I've removed that conditional and everything passes. So I'm not quite sure what fixed it!
Best, Doug