IT TIP

#에 대한 정의되지 않은 메소드`get '

itqueen 2020. 10. 22. 23:47
반응형

#에 대한 정의되지 않은 메소드`get '


누구든지이 문제를 해결하는 방법을 알고 있습니까? OSX에서 Rails 3.0.7로 RSpec을 실행하려고합니다. 자세한 내용 : https://gist.github.com/1017044

  it "renders buttons_widgets partial" do
    get :buttons_widgets
    response.should render_template("buttons_widgets")
  end


→ rspec tools_model_spec.rb
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/version.rb:4: warning: already initialized constant STRING
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/metadata.rb:48: warning: already initialized constant RESERVED_KEYS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/pending.rb:6: warning: already initialized constant DEFAULT_MESSAGE
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:6: warning: already initialized constant PROC_HEX_NUMBER
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:7: warning: already initialized constant PROJECT_DIR
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:43: warning: already initialized constant CONDITIONAL_FILTERS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:48: warning: already initialized constant DEFAULT_BACKTRACE_PATTERNS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/runner.rb:13: warning: already initialized constant AT_EXIT_HOOK_BACKTRACE_LINE
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core.rb:35: warning: already initialized constant SharedContext
Run filtered excluding {:if=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:43>, :unless=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:44>}
F

Failures:

  1) ToolsController renders buttons_widgets partial
     Failure/Error: get :buttons_widgets
     NoMethodError:
       undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>
# ./tools_model_spec.rb:7:in `block (2 levels) in <top (required)>'

RSpec은 사양이 컨트롤러 사양인지 알지 못하므로 예제는 get메서드에 액세스 할 수 없습니다 .

RSpec 2.x는 controllers 디렉토리의 모든 것이 컨트롤러 사양이라고 가정합니다.

이것은 RSpec 3에서 변경되었습니다.

파일 형식 추론은 기본적으로 비활성화되어 있습니다.

이전에는 파일 위치에서 사양 유형을 자동으로 추론했습니다. 이것은 새로운 사용자에게는 놀라운 동작이었고 일부 베테랑 사용자에게는 바람직하지 않았으므로 RSpec 3부터는이 동작을 명시 적으로 선택해야합니다.

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-inference-disabled

에서 README를 RSpec에-레일 :

컨트롤러 사양은 기본적으로 spec/controllers folder. 메타 데이터로 컨텍스트에 태그를 지정하면 :type => :controller예제를 컨트롤러 사양으로 취급합니다.

RSpec에 대한 컨트롤러 컨텍스트 메타 데이터 설정의 예 :

describe ToolsController, :type => :controller do
    # ...
end

'spec / features'를 사용하는 경우 'spec_helper.rb'에 다음을 추가해야 할 수 있습니다.

config.include RSpec::Rails::RequestExampleGroup, type: :feature

Rspec 3.x에서 사양 유형은 파일 위치에서 자동으로 추론되지 않으므로 수동으로 설정해야하며이를 spec_helper.rb에 추가해야합니다.

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

Rspec 업그레이드


추가하여 내 앱에서이 문제를 해결할 수있었습니다. require 'rspec/rails'내 spec_helper 파일 하여 내 .


이것을 조사하는 다른 사람들을 위해. undefined method 'get'오류 를 추적하려고했습니다 . 내 문제는 내가했다이었다 getA의 describe block있는지 확인get 에를 it block.


spec 폴더 _spec.rb 파일에서
describe PagesController do로 대체하여 해결되었습니다 . 또한 중단 경고 사용을 방지하기 위해 대신을 .RSpec.describe PagesController, :type => :controller do

expect(response).to be_successresponse should be_success
추신 : 추가 할 필요가 없었습니다 require "rails_helper".


require 'spec_helper'내 사양 파일의 맨 위에 추가 하는 것을 잊었을 때이 오류가 발생했습니다.--require spec_helper .rspec 파일 .


파일 rspec을 생성하는 데 사용한 경우 다음 .rspec에서 내용을 변경해야합니다.

--require spec_helper

에:

--require rails_helper

대안은 type: :request사양 을 지정 하는 것입니다. 예를 들면 :

RSpec.describe "Widget management", :type => :request do

  it "creates a Widget and redirects to the Widget's page" do
    get "/widgets/new"
    expect(response).to render_template(:new)

    post "/widgets", :widget => {:name => "My Widget"}

    expect(response).to redirect_to(assigns(:widget))
    follow_redirect!

    expect(response).to render_template(:show)
    expect(response.body).to include("Widget was successfully created.")
  end

end

여기에서 가져온 예제 https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec .


추가 할 때이 문제가 발생했습니다.

gem 'rspec'

rails 프로젝트의 내 Gemfile에. 그것은해야한다

gem 'rspec'
gem 'rspec-rails'

(또는 rspec-rails).

bundle install

다음을 사용하여 사양 디렉토리를 다시 만듭니다.

rspec --init

and put your xxx_spec.rb file in the appropriate directory (won't work if it is in the spec directory). Beginners error but maybe this helps somebody ;) Here's the link that helped me:

https://www.relishapp.com/rspec/rspec-rails/docs/gettingstarted


this can happen under the following conditions:

  1. your spec does not have :type => :controller [type: :controller in newer Ruby]

  2. your spec is not in the controllers folder or you not have set config.infer_spec_type_from_file_location!

Either #1 or #2 must be setup for your spec. Also, this can happen under this condition as well:

  1. you have written a spec using the old-style require 'spec_helper' instead of using the newer require 'rails_helper'. You will note that rails_helper now includes spec_helper (to generate both see the Rspec installation steps)

cross referencing GH issue https://github.com/rails/rails-controller-testing/issues/36

참고URL : https://stackoverflow.com/questions/6296235/undefined-method-get-for-rspeccoreexamplegroupnested-10x00000106db51f

반응형