【问题标题】:RSpec 3 Error undefined method `get' in request test请求测试中的 RSpec 3 错误未定义方法“get”
【发布时间】:2014-06-24 08:41:58
【问题描述】:

在 RSpec 3 迁移后请求测试失败。 这是我的测试

describe 'GET /api/v1/activities' do
    let!(:user) { create(:user) }

    subject { json['activities'] }

   context 'when not authenticated' do
      let(:token) { user.authentication_token }
      let(:email) { 'unregistered.user@mail.com' }
      before :each do
        get '/api/v1/activities', {}, {token: token, email: email}
      end
      it { expect(response).to_not be_success }
    end
end

这是 rspec 日志

Activities GET /api/v1/activities when not authenticated 
     Failure/Error: get '/api/v1/activities', {}, {token: token, email: email}
     NoMethodError:
       undefined method `get' for #<RSpec::ExampleGroups::Activities::GETApiV1Activities::WhenNotAuthenticated:0x0000000becfaf8>

会是什么?

【问题讨论】:

    标签: ruby-on-rails testing rspec request rspec3


    【解决方案1】:

    好的,迁移到 RSpec3 是导致此问题的原因。 我通过迁移到 RSpec 2.99.0 解决了这个问题,我收到了将此代码添加到我的 spec_helper 的提示

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

    现在可以了。万岁!

    【讨论】:

      【解决方案2】:

      我已经设置了infer_spec_type_from_file_location!,但我们这里仍然使用capybara 特征语法,它可能将类型设置为特征。一旦我将feature 更改为describe,它就起作用了。

      【讨论】:

        【解决方案3】:

        尝试添加帮助器来请求规范。在spec_helper.rb:

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

        我还假设您的规范定义中有type: :request。

        【讨论】:

        • 我在spec_helperconfig.include Requests::JsonHelpers, type: :request中有这个字符串,不一样吗?此外,我已经找到了对我有帮助的解决方案,但我没有足够的声誉同时发布它。你认为我和你的解决方案是相对的吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多