Ruby Rails Tutorial Section 10.4.2 Test

After completing Listing 10.40 and running rspec / spec / controllers / users_controller_spec.rb, I got 1 failure for the section (all other tests in the destroy section are passed)

describe "DELETE 'destroy'" do

  before(:each) do
    @user = Factory(:user)
  end

  describe "as a non-signed-in user" do
    it "should deny access" do
      delete :destroy, :id => @user
      response.should redirect_to(signin_path)
    end
  end
end

Here is the console output:

Failures:
  1) UsersController DELETE 'destroy' as a non-signed-in user should deny access
     Failure/Error: delete :destroy, :id => @user
     undefined method `admin?' for nil:NilClass
     # ./app/controllers/users_controller.rb:66:in `admin_user'
     # ./spec/controllers/users_controller_spec.rb:282:in `block (4 levels) in <top (required)>'

I canโ€™t understand if there is a mistake in the textbook code, or if I made a mistake somewhere.

+3
source share
3 answers

I canโ€™t say for sure, but it seems that this is part of the textbook code, resulting in

<% if current_user.admin? %>

If no one is signed on this page, current_userit is nil (I assume) and therefore cannot have a method admin?.

Try replacing this with

<% if current_user && current_user.admin? %>

( current_user ).

?

, , ?


, : , TDD- . :

  • .

, . 10.41 , .

, ,

def admin_user
  redirect_to(root_path) unless current_user && current_user.admin?
end

( , current_user, , ).

+3

, , , :destroy :authenticate , 10.11, (: .)

+5

I read this tutorial and I also came across this unsuccessful test.

Check if you have:

before_filter :authenticate, :only => [:index, :edit, :update, :destroy]

instead:

before_filter :authenticate, :only => [:index, :edit, :update]

in users_controller.rb

+1
source

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


All Articles