Michael Hartl Rails Tutorial: Rspec "Expected response to redirect to ..." is incorrect

In Michael Hartl's (wonderful) Rails Tutorial, I get an unexpected failure of an Rspec form test:

"The expected answer is to redirect to < http://test.host/signin >, but redirect < http://test.host/signin?notice=Please+sign+in+to+access+this+page. >.

(Find this in Section 10.3.) So, because of the error itself, you can indicate that the server is being redirected to the corresponding page, except that there is an additional notification for "Please Login." The test code is as follows:

describe "GET 'index'" do describe "for non-signed-in users" do it "should deny access" do get :index response.should redirect_to(signin_path) flash[:notice].should =~ /sign in/i end end ... 

Am I doing something wrong here? What is the best way to fix this?


Update:

If I changed the code to

 response.should redirect_to('http://test.host/signin?notice=Please+sign+in+to+access+this+page.') 

then I get the actual ruby ​​error

Error / Error: Flash [: notice] .should = ~ / sign in / i Expected: / sign / i, got: nil (using = ~)


Resolution

The problem was a missing comma in my deny_access function:

 redirect_to signin_path :notice => "Please sign in to access this page." 

should be

 redirect_to signin_path, :notice => "Please sign in to access this page." 

This is fixed. Now the side effect of this fix was interesting, right? Does this mean that signin_path is the function itself that can take a hash or arguments that will be added to the end of the path? Strange, wild and wonderful.

+4
source share
1 answer

What do your authentication methods look like? I have not yet seen a flash notification be added to the query string as before: / This makes me believe that your methods may be wrong. It should be something like:

 def deny_access redirect_to signin_path, :notice => "Please sign in to access this page." end 

OR

 def deny_access flash[:notice] = "Please sign in to access this page." redirect_to signin_path end 
+2
source

Source: https://habr.com/ru/post/1337066/


All Articles