Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

jadaml
jadaml
11,593 Points

Requiring Login - Testing Failure - Build Simple Rails App

I get the following error to the recently added test_should_be_redirected_when_not_logged_in(StatusesControllerTest) [test/functional/statuses_controller_test.rb:16]:

Expected response to be a <:redirect>, but was <200>

Can anyone explain to me why I'm getting this.

4 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Jadam L,

I'm not sure why you're getting this error, but Jason Seifer should be able to answer your question. He's speaking at a web conference this week, but he should be able to get back to you soon! Sorry for the delay, but Jason will have the best answer. :)

Nick

Yonatan Schultz
Yonatan Schultz
12,045 Points

Looks like your Statuses controller is not checking to see if the user is logged in prior to rendering the Status.new

Check out statuses_controller.rb and make sure that your before_filter is filtering for :new as below:

before_filter :authenticate_user!, only [:new, :create, :edit, :update]

I hope that helps!

Itay Banner
Itay Banner
1,922 Points

I'm getting something quite similar. The test is written exactly like it's written in the video itself:

test "should be logged in to post a status" do
  sign_in users(:itay)
  post :create, status: { content: "Hello. This is a test status" }
  assert_response :redirect
  assert_redirected_to new_user_session_path # <=This is the failure line
end

But somehow it fails:

Expected response to be ab redirect to <http://test.host/users/sign_in> but was a redirect to <http://test.host/statuses/980190963>

And my before_filter is updated just like Yonatan here said. And the wierdest thing is, when I take the server up and go to the app itself, I am redirected to /users/sign_in when I try to post a new message

Any ideas?

Itay Banner
Itay Banner
1,922 Points

I'm such a moron. Of course the test failed, because I forgot to remove the "sign in users(:user)" line. Can't remember now how it got there.

Removed it and all's fine now.

Jadam L , maybe you've got the same symptoms? :-)