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 Creating Friendships What is a Join Table?

Lisa Rossiter
Lisa Rossiter
3,630 Points

Testing errors

This isn't working again on rails 4, I will paste in my code and errors received.

def update
  @status = current_user.statuses.find(params[:id])
  if params[:status] && params[:status].has_key?(:user_id)
    params[:status].delete(:user_id)
  end
  respond_to do |format|                                                             48
    if @status.update(status_params)                                         49
      format.html { redirect_to @status, notice: 'Status was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @status.errors, status: :unprocessable_entity }
    end
  end
end
test "should not update is status has not changed" do
    sign_in users(:john)
    patch :update, id: @status

    assert_redirected_to status_path(assigns(:status))
    assert_equal assigns(:status).user_id, users(:john).id
  end
# Running tests:

.......F...E.EE

Finished tests in 0.339993s, 44.1186 tests/s, 79.4134 assertions/s.

  1) Failure:
StatusesControllerTest#test_should_create_status_for_a_current_user_when_logged_in [test/controllers/statuses_controller_test.rb:60]:
"Status.count" didn't change by 1.
Expected: 3
  Actual: 2

  2) Error:
StatusesControllerTest#test_should_not_update_is_status_has_not_changed:
ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?]
    app/controllers/statuses_controller.rb:45:in `update'
    test/controllers/statuses_controller_test.rb:78:in `block in <class:StatusesControllerTest>'

  3) Error:
StatusesControllerTest#test_should_update_status:
ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?]
    app/controllers/statuses_controller.rb:45:in `update'
    test/controllers/statuses_controller_test.rb:98:in `block in <class:StatusesControllerTest>'

  4) Error:
StatusesControllerTest#test_should_update_status_for_a_current_user_when_logged_in:
ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?]
    app/controllers/statuses_controller.rb:45:in `update'
    test/controllers/statuses_controller_test.rb:70:in `block in <class:StatusesControllerTest>'

15 tests, 27 assertions, 1 failures, 3 errors, 0 skips
Lisa Rossiter
Lisa Rossiter
3,630 Points

Its strange because there is no user id being picked up...

4 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Lisa Rossiter the error in the test is that the status can't be found with the given id and user_id. Try printing out @status.user_id and users(:john).id and make sure they match. That is most likely the cause of one of the failures.

Lisa Rossiter
Lisa Rossiter
3,630 Points

Ok I will try that. I am a bit confused because I thought we was deleting user id as shown below?

if params[:status] && params[:status].has_key?(:user_id)
    params[:status].delete(:user_id)
  end
Lisa Rossiter
Lisa Rossiter
3,630 Points

I did actually clear my status table to start fresh(Got a bit carried away creating statuses) which was after we got rid of the dropdown in the new view. Would that make a difference its just strange as its showing the below:

[WHERE "statuses"."user_id" = ?]

Having the same issue with this... did you ever figure it out?

Lisa Rossiter
Lisa Rossiter
3,630 Points

Yes the problem was with my fixtures. You have to set it up in the correct way it took ages to find my bug. It wasn't like a rails specific bug it was that I didn't realise how strict the order of setting up friends in your test had to be. Post up your failing test file and I will take a look for you.

Mike Mayer
Mike Mayer
2,151 Points

Lisa Rossiter did you ever find a solution to this problem? I'm struggling with the same issue. Any chance you code is on Github?

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Does the @status in your test belong to the john user from your test?

Lisa Rossiter
Lisa Rossiter
3,630 Points

Well I signed him in as above, could you explain more?