ruby-on-rails - RSpec 错误 "undefined method ` respond_with'..."

标签 ruby-on-rails ruby rspec

我正在学习教程 http://apionrails.icalialabs.com/book/chapter_three 但是当我运行 Controller 测试时 bundle exec rspec spec/controllers 我得到了未定义的方法错误:

Failures:

 Api::V1::UsersController GET #show 
 Failure/Error: it { should respond_with 200 }
 NoMethodError:
   undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000005d6f650>
 # ./spec/controllers/api/v1/users_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

 Finished in 0.08435 seconds
 2 examples, 1 failure

请帮忙

编辑: 以下是我的规范文件的内容

users_controller_spec.rb

require 'spec_helper'

describe Api::V1::UsersController do
  before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }

  describe "GET #show" do
    before(:each) do
      @user = FactoryGirl.create :user
      get :show, id: @user.id, format: :json
    end

    it "returns the information about a reporter on a hash" do
      user_response = JSON.parse(response.body, symbolize_names: true)
      expect(user_response[:email]).to eql @user.email
    end

    it { should respond_with 200 }
  end
end

最佳答案

这里因为我们正在处理响应代码,你可以使用-

它{ expect(response).to have_http_status(200) }

而不是 它 { should respond_with 200 }

这里也不需要 shoulda-matchers gem。

Ref - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs

关于ruby-on-rails - RSpec 错误 "undefined method ` respond_with'...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35753438/

相关文章:

ruby-on-rails - 未检测到 Procfile,使用默认 Web 服务器 (webrick)

ruby-on-rails - 如何使用Shoulda Matchers测试多态关联?

ruby-on-rails - 如何使用 ruby​​ 2.0 运行 Ruby on Rails 3

ruby-on-rails - rspec Rails 模拟 session 哈希

ruby-on-rails - 文件和目录结构的 RSpec 命名约定

ruby-on-rails - Docker - 无法转发交互式 shell 的端口

ruby-on-rails - Rails 中的可选或条件模型关联

ruby - Time.now 与 Controller 的 Rails 问题

javascript - AngularJS 和 ui-view 导致无限递归

ruby-on-rails - Rails respond_with & Rspec Controller : Testing unsuccessful update