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 Build a Simple Ruby on Rails Application Testing the Whole App Testing the Statuses Controller

Rodrigo Soares
Rodrigo Soares
2,460 Points

Test if failing while it should be passing

Hello guys.

I wrote the code to test that the user must be logged in in order to post any statuses.

The results in my terminal are showing me I have problems with the redirect assertion but it looks ok to me.

I used the Devise Helpers and set the before_filter with authentication method in it (only for :new and :create}.

As I said, my code seems to be right to me and can find out what's wrong.

My testing code (statuses_controller_test.rb) ''' test "Should be logged in to post statuses" do

sign_in users(:rodrigo) post :create, status: { content: "Hello" } assert_response :redirect assert_redirected_to new_user_session_path

end ''' The before_filter authentication (statuses_controller.rb) ''' before_filter :authenticate_user!, only: [:new, :create] ''' test_helper file that contains the code from Devise:

''' class ActionController::TestCase include Devise::TestHelpers end '''

My test result in the command line:

''' 1) Failure: StatusesControllerTest#test_Should_be_logged_in_to_post_statuses [test/controllers/statuses_controller_test.rb:34]: Expected response to be a redirect to http://test.host/users/sign_in but was a redirect to http://test.host/statuses/980190963. Expected "http://test.host/users/sign_in" to be === "http://test.host/statuses/980190963".

9 tests, 19 assertions, 1 failures, 0 errors, 0 skips '''

Any help would be appreciated !

1 Answer

Rodrigo Soares
Rodrigo Soares
2,460 Points

Actually the mistake was under my nose (brazilian expression, I'm not sure if you get it). Anyway , my test failed because in the test I assume that the user is already logged in so he should be successfully redirected to the statuses page.

Nothing like a 30 min break to see things more clearly.