LoginSignup
15
17

More than 5 years have passed since last update.

GETリクエストをするspecを実行して"undefined method `get'"と怒られた時は

Last updated at Posted at 2014-09-30

現象

require 'rails_helper'

describe 'あいさつ' do
  context '朝は' do
    it 'おはようと返すこと' do
      get '/geeting/morning'
      expect(response.body).to include('おはよう')
    end
  end
end
$ bundle exec rspec spec/greet_spec.rb
undefined method `get' for #<RSpec::ExampleGroups::Nested::Nested0:0x007fe53ec7fee0>

*_spec.rbのパスに注目

spec/greet_spec.rb

ここはだめ

spec/requests/greet_spec.rb

ここはOK

もしくは:type metadataで明示的に指定する

require 'rails_helper'

describe 'あいさつ', type: :request do
  context '朝は' do
    it 'おはようと返すこと' do
      get '/geeting/morning'
      expect(response.body).to include('おはよう')
    end
  end
end

逆にCapybara#visitは...

spec/features/

に置くか、

require 'rails_helper'

describe 'あいさつ', type: :feature do
  context '朝は' do
    it 'おはようと返すこと' do
      get '/geeting/morning'
      expect(response.body).to include('おはよう')
    end
  end
end

type: :featureを指定します。

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17