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

Robert Ho
PLUS
Robert Ho
Courses Plus Student 11,383 Points

Creating Friendships: What is a join table? Tests not passing

The first test in this video isn't passing for me even after following the instructions. I changed my instance variable from @status = Status.new(params[:status]) to current_user.statuses.new(params[:status]) but I'm still getting this error:

1) Failure: test_should_create_status_for_the_current_user_when_logged_in(StatusesControllerTest) [test/functional/statuses_controller_test.rb:55]: <1027431151> expected but was <149087659>.

Any suggestions on how to make the test pass? For a reference to my test:

test "should create status for the current user when logged in" do
sign_in users(:jason)

assert_difference('Status.count') do
  post :create, status: {content: @status.content, user_id: users(:jim).id }
end

assert_redirected_to status_path(assigns(:status))
assert_equal assigns(:status).user_id, users(:jason).id 

end // should be at the end of the code block don't know why it's not but am new using Mark Down

I have a feeling that the reason it isn't passing has something to do with the test itself. And for my method within the StatusController:

def create
@status =  current_user.statuses.new(params[:status])

respond_to do |format|
  if @status.save
    format.html { redirect_to @status, notice: 'Status was successfully created.' }
    format.json { render json: @status, status: :created, location: @status }
  else
    format.html { render action: "new" }
    format.json { render json: @status.errors, status: :unprocessable_entity }
  end
end

end // also should be at the end of the code block don't know why it's not but am new using Mark Down

Any tips or suggestions on what is wrong? Thanks so much!

4 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Well in your test you are assigning the user_id to jim and not jason. So I'm not sure what you're testing, but according to the way you wrote the test, I think it should fail. This line:

assert_equal assigns(:status).user_id, users(:jason).id 

Says, is the user_id on the newly created status jason's id? But, you assigned the user_id on status to jim:

post :create, status: {content: @status.content, user_id: users(:jim).id }
Robert Ho
PLUS
Robert Ho
Courses Plus Student 11,383 Points

you know what I thought about that too but I double checked the video and thats what Jason Seifer did. Im still new to RubyOnRails so I wasn't too sure what the test was testing for. Are you saying the code should look like this? assert_difference('Status.count') do post :create, status: {content: @status.content, user_id: users(:jason).id } end

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Robert,

Have you made any new discoveries on this issue? I just ran into the same scenario tonight.

My thought is the reason the id for Jim is passed to create is to prove that Jim's id would be ignored and instead would use the currently logged in user's id, in this case for Jason.

However, even after making the changes noted in the video I too get the same failure. I'm tagging Jason Seifer on this one in hopes he can lead us in the right direction.

Sean Perryman
Sean Perryman
13,810 Points

That is actually the problem I just ran into myself. You are correct in asserting that trying to force the jim fixture in there is to show that it will ignore it when trying to be inserted.

Change your @status = current_user.statuses.new(params[:status]) to @status = current_user.statuses.new(status_params) to get the create test to pass. However, doing this causes the edit and destroy tests to fail and that is what I am currently stuck on.

Sean Perryman
Sean Perryman
13,810 Points

Forgot one other; for the update method add this above the respond_to statement: params[:status].delete(:user_id) if params[:status].has_key?(:user_id) as Jason Seifer did in the video. Down at the bottom of the controller, in the private block, change the set_status method to read: @status = current_user.statuses.find(params[:id]). That's where I am at currently, and only the edit and destroy tests are failing. You can look at this full code base at my github account: http://github.com/sean-perryman/treebook

Sean Perryman
Sean Perryman
13,810 Points

I changed my set_status back to @status = Status.find(params[:id]), and took the @status = current_user.statuses.find(params[:id]) and it fixed my edit and destroy test problems. Yay!