IT story

모든 Ruby 테스트 제기 : nil : NilClass에 대해 정의되지 않은 메소드 'authenticate'

hot-time 2020. 7. 1. 07:49
반응형

모든 Ruby 테스트 제기 : nil : NilClass에 대해 정의되지 않은 메소드 'authenticate'


내 테스트의 대부분은 다음을 제기하고 있으며 이유를 이해하지 못합니다. 모든 메소드 호출은 '인증'오류를 발생시킵니다. "authenticate"라는 메소드가 있는지 코드를 확인했지만 그러한 메소드는 없습니다.

  1) Admin::CommentsController handling GET to index is successful
     Failure/Error: get :index
     undefined method `authenticate!' for nil:NilClass
     # ./spec/controllers/admin/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'


  124) PostsController handling GET for a single post should render show template
     Failure/Error: get :show, :year => '2008', :month => '01', :day => '01', :slug => 'a-post'
     undefined method `authenticate' for nil:NilClass
     # ./app/controllers/application_controller.rb:18:in `set_current_user_for_model'
     # ./spec/controllers/posts_controller_spec.rb:131:in `do_get'
     # ./spec/controllers/posts_controller_spec.rb:140:in `block (3 levels) in <top (required)>'

테스트를 직접 실행하려는 경우 프로젝트를 찾을 수 있습니다 => https://github.com/agilepandas/enki


이 질문은 @MatthewClosson 님에 의해 Twitter에서 답변되었습니다

@jeffehh 여기에 지정된대로 spec / support / devise.rb 파일을 만들어야합니다. https://github.com/plataformatec/devise#test-helpers 고안된 테스트 도우미를 포함하려면 #ruby

다시 한번 감사합니다.


Rspec을 사용하고 있음을 알고 있지만와 동일한 문제가 발생할 수 있습니다 Test::Unit. 고안된 테스트 도우미를 추가하기 만하면됩니다.test/test_helper.rb

class ActiveSupport::TestCase
  include Devise::TestHelpers
end

위의 답변이 저에게 효과가 없었습니다 (RSpec 3.1).

나를 위해 일한 솔루션 https://stackoverflow.com/a/21166482/1161743참조 하십시오 .

변수를 설정하기 전에 익명 사용자를 로그 아웃해야합니다.

before :each do
  sign_out :user
end

RSpec에서

Jeffrey W.가 언급했듯이 위의 대답에서 -> 이것을 모든 컨트롤러로 설정하십시오.

RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, type: :controller
  # ...
end

그러나 이것이 하나의 사양에만 관련되는 경우 반드시 모든 컨트롤러 사양에 고안 도우미를 포함시킬 필요는 없으며 해당 컨트롤러 도우미 블록에 도우미를 명시 적으로 포함시킬 수 있습니다.

require 'spec_helper'
describe MyCoolController
  include Devise::TestHelpers

  it { } 
end

내 프로젝트 중 하나에서 동일한 오류가 발생했습니다. Ruby 2.0.0-p598, Rails 3.2.21, RSpec 2.99를 사용하고 있습니다. 모든 사양을 함께 실행하면 문제가 발생했습니다. 사양을 개별적으로 실행하면 통과했습니다. 내 spec / spec_helper.rb에 다음이 포함되어 있습니다.

RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, type: :controller
  # ...
end

실패한 각 스펙 파일의 첫 번째 설명에 다음을 추가했습니다. 이렇게해도 문제가 해결되지 않았습니다.

before :each do
  sign_out :user
end

둘 다하지 않았다 :

after :each do
  sign_out :user
end

stackoverflow 질문에 대한 답변에서 영감을 얻은 rspec 디렉토리의 서로 다른 조합을 함께 실행하여 서로 방해 할 수있는 디렉토리를 찾았습니다. 결국 나는 내가 전화하고 있음을 발견했다.

before() do #note no :each passed to before
  :
end

이 모든 발생을 다음과 같이 변경했을 때 :

before(:each) do
  :
end

모든 사양은 실패없이 통과되었습니다.

undefined method `authenticate' for nil:NilClass

나는 이것이 다른 사람들에게 도움이되기를 바랍니다.


If you're working with view spec, you can stub of current_user. This effectively overrides the current_user helper called from your view with whatever is returned. Here's how with rspec-3.2.3:

RSpec.describe "projects/show", type: :view do
  before(:each) do
    allow(view).to receive(:current_user).and_return(FactoryGirl.create(:user))
  end

  it "renders attributes in <p>" do
    render
    expect(rendered).to match(/whatever you want to regex match here/)
  end
end

It looks like there are some updates to the source code. The ApplicationController specifies that there needs to be an authenticate_user! filter run before any request. This thread provides some background on issues with it:

http://groups.google.com/group/plataformatec-devise/browse_thread/thread/f7260ebe2d9f7316?fwc=1

Essentially, the authenticate_user! function is part of Rails 3 (using the new devise feature, of which I know little about). If the app can't find the User model (either because of namespace issues or whatever reason) then the method will fail. The "enki" application you linked to is now a Rails 3 application. It could be experiencing a few growing pains as it converts over.


Ruby is telling you, that method #authenticate has not been definded on nil yet. You can do it easily by:

def nil.authenticate!
  puts "Bingo! Nil is now authentic!"
end

And the error will go away.

참고URL : https://stackoverflow.com/questions/4308094/all-ruby-tests-raising-undefined-method-authenticate-for-nilnilclass

반응형