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

Statuses Controller Test - Expected response to be a <:redirect>, but was <200> and undefined method 'statuses' for nil

I am receiving an failure when testing my statuses controller

  1. $ ruby -I test test/functional/statuses_controller_test.rb

  2. Failure: test_should_not_update_the_status_if_nothing_has_changed(StatusesControllerTest) [test/functional/statuses_controller_test.rb:110]: Expected response to be a <:redirect>, but was <200>

Failure: test_should_update_status_for_the_current_user_when_logged_in(StatusesControllerTest) [test/functional/statuses_controller_test.rb:103]: Expected response to be a <:redirect>, but was <200>

Failure: test_should_update_status_when_logged_in(StatusesControllerTest) [test/functional/statuses_controller_test.rb:97]: Expected response to be a <:redirect>, but was <200>

  1. My controller test: test "should update status when logged in" do sign_in users(:jason) put :update, id: @status, status: { content: @status.content } assert_redirected_to status_path(assigns(:status)) end

test "should update status for the current user when logged in" do sign_in users(:jason) put :update, id: @status, status: { content: @status.content, user_id: users(:jim).id } assert_redirected_to status_path(assigns(:status)) assert_equal assigns(:status).user_id, users(:jason).id end

test "should not update the status if nothing has changed" do sign_in users(:jason) put :update, id: @status assert_redirected_to status_path(assigns(:status)) assert_equal assigns(:status).user_id, users(:jason).id end

  • It looks like it can't find the status_path, but I redirect to that in other tests. Not sure what is going on. The videos didn't run into this and I couldn't find a forum answer.

ALSO, when running running bin/rake I get this error (that may have to do with it...) test_should_destroy_status(StatusesControllerTest): NoMethodError: undefined method `statuses' for nil:NilClass

Could you post your StatusController code?

Have you migrated the test database after adding statuses?

$ rake db:migrate db:test:prepare

Yes, that got rid of the error---- NoMethodError: undefined method `statuses' for nil:NilClass which is great, but I still have the failures on---- assert_redirected_to status_path(assigns(:status))

It's saying its looking to redirect to the status page of the current signed in user, correct? I feel like my code reflects that path, but it is sending back a 200 page.

I really appreciate you looking into this, thanks!

1 Answer

class StatusesController < ApplicationController before_filter :authenticate_user!, only: [:new, :create, :edit, :update]

# GET /statuses # GET /statuses.json def index @statuses = Status.order('created_at desc').all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @statuses }
end

end

# GET /statuses/1 # GET /statuses/1.json def show @status = Status.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @status }
end

end

# GET /statuses/new # GET /statuses/new.json def new @status = current_user.statuses.new @status.build_document

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @status }
end

end

# GET /statuses/1/edit def edit @status = current_user.statuses.find(params[:id]) end

# POST /statuses # POST /statuses.json 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

# PUT /statuses/1 # PUT /statuses/1.json def update @status = current_user.statuses.find(params[:id]) @document = @status.document if params[:status] && params[:status].has_key?(:user_id) params[:status].delete(:user_id) end respond_to do |format| if @status.update_attributes(params[:status]) && @document && @document.update_attributes(params[:status][:document_attributes]) 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

# DELETE /statuses/1 # DELETE /statuses/1.json def destroy @status = current_user.statuses.find(params[:id]) @status.destroy

respond_to do |format|
  format.html { redirect_to statuses_url }
  format.json { head :no_content }
end

end end