获取未定义的方法`get'for#<rspec :: examplegroups in =“”my =“”rspec =“”

时间:2016-04-02 23:42:16

标签: ruby-on-rails ruby testing rspec

=“”

Rails 4.2。 5,rspec-rails 3.0.2

我想测试我的API。所以我在/ spec中创建了requests目录。有一个名为projects_spec.rb

的文件

以下是代码:

describe 'Projects API' do
  describe 'GET /projects' do
    it 'should return 401 HTTP code' do
      get '/api/v1/projects'
      expect(response.status).to eq(401)
    end
  end
end

当我运行此测试时,我正在

NoMethodError: undefined method `get' for #RSpec::ExampleGroups::ProjectsAPI::GETProjects:0x007fee73ad9b48>

怎么了?

# rails_helper.rb
config.infer_spec_type_from_file_location!

1 个答案:

答案 0 :(得分:6)

describe 'Projects API', type: :request do
  # ...
end

另外,请确保require 'rails_helper'中包含projects_spec.rb