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 trialPraveen Boddu
1,669 PointsWhat is State Machine - Stuck at send_request_email
Below is the error.
test: #send_request_email should send and email. (UserFriendshipTest):
ActiveRecord::RecordNotFound: Couldn't find UserFriendship without an ID
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:312:in find_with_ids'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:107:in
find'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/querying.rb:5:in find'
/Users/praveenboddu/Documents/Projects/treebook/app/mailers/user_notifier.rb:5:in
friend_requested'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:167:in process_action'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:121:in
process'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in process'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionmailer-3.2.13/lib/action_mailer/base.rb:459:in
process'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionmailer-3.2.13/lib/action_mailer/base.rb:453:in initialize'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionmailer-3.2.13/lib/action_mailer/base.rb:439:in
new'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionmailer-3.2.13/lib/action_mailer/base.rb:439:in method_missing'
/Users/praveenboddu/Documents/Projects/treebook/app/models/user_friendship.rb:13:in
send_request_email'
test/unit/user_friendship_test.rb:36:in block (3 levels) in <class:UserFriendshipTest>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/testing/assertions.rb:55:in
assert_difference'
test/unit/user_friendship_test.rb:35:in block (2 levels) in <class:UserFriendshipTest>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:in
call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:in block in create_test_from_should_hash'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/mocha-0.13.3/lib/mocha/integration/mini_test/version_230_to_2101.rb:36:in
run'
this is the model
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
attr_accessible :user, :friend, :user_id, :friend_id
state_machine :state, initial: :pending do
end
def send_request_email
UserNotifier.friend_requested(id).deliver
end
end
5 Answers
Michael Stenzler
4,147 PointsPraveen. I encountered the same problem because in the setup of the test, the example was using new to create @user_friendship. on line 30 of user_friendship_test.rb make sure it reads:
context "#send_request_email" do
setup do
@user_friendship = UserFriendship.create user: users(:jason), friend: users(:mike)
end
and not
setup do
@user_friendship = UserFriendship.new user: users(:jason), friend: users(:mike)
end
Jason Seifer
Treehouse Guest TeacherHey Praveen, can you post the user friendship test file here too?
Praveen Boddu
1,669 PointsHey Jason,
Below is my test file. Rest of the tests passes. I wrote tests for accepting friendship as well and they pass without any issue.
require 'test_helper'
class UserFriendshipTest < ActiveSupport::TestCase
should belong_to(:user) should belong_to(:friend)
test "creating a friendship works without raising an exception" do assert_nothing_raised do UserFriendship.create user: users(:rajesh), friend: users(:peter) end end
test "that creating a friendship based on user id and friend id works" do UserFriendship.create user_id: users(:rajesh).id, friend_id: users(:peter).id assert users(:rajesh).friends.include?(users(:peter)) end
context "a new instance" do setup do @user_friendship = UserFriendship.new user: users(:rajesh), friend: users(:peter) end
should "have a pending state" do
assert_equal 'pending', @user_friendship.state
end
end
context "#send_request_email" do setup do @user_friendship = UserFriendship.new user: users(:rajesh), friend: users(:peter) end
should "send an email" do
assert_difference 'ActionMailer::Base.deliveries.size', 1 do
@user_friendship.send_request_email
end
end
end
context "#accept!" do setup do @user_friendship = UserFriendship.new user: users(:rajesh), friend: users(:peter) end
should "set the state to accepted" do
@user_friendship.accept!
assert_equal "accepted", @user_friendship.state
end
should "send an acceptance email" do
assert_difference 'ActionMailer::Base.deliveries.size', 1 do
@user_friendship.accept!
end
end
should "inculde the friend in the list of friends" do
@user_friendship.accept!
users(:rajesh).friends.reload
assert users(:rajesh).friends.include?(users(:peter))
end
end end
Praveen Boddu
1,669 PointsAh..That makes total sense. Thanks Mike. What I don't understand is how the test got passed in the video?
Alejandro Cárdenas
4,777 PointsI had the same problem! It was killing me. Thanks!
Ben Rubin
Courses Plus Student 14,658 PointsBen Rubin
Courses Plus Student 14,658 PointsThanks Michael. I had the same problem and it was driving me crazy. Your solution worked perfectly.