【问题标题】:RSpec tests failing with undefined method `respond_with'RSpec 测试失败,未定义方法“respond_with”
【发布时间】:2020-01-06 10:23:55
【问题描述】:

我正在处理 Rails API Tutorial 并遇到错误。每当我运行测试时,我都会得到

  1) Api::V1::UsersController PUT/PATCH #update when is not created
     Failure/Error: it { should respond_with 422 }

     NoMethodError:
       undefined method `respond_with' for #<RSpec::ExampleGroups::ApiV1UsersController::PUTPATCH
Update::WhenIsNotCreated:0x007f860e070eb8>
       Did you mean?  respond_to

我发现另外两个帖子(thisthis)指向一个非常相似的问题,但都没有解决。有人对此有解决方案吗?谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    正如提到的here,而不是:

    it { should respond_with 422 }
    

    试试这个:

    it { expect(response).to have_http_status(422) }
    

    【讨论】:

    • 谢谢你。结果是shoulda-matchers 3+,他们将匹配器的范围限定为他们的规格类型。所以控制器规格会得到控制器匹配器而不是模型匹配器等。respond_with 是控制器匹配器。因此,如果您在 Controller 规范之外运行断言,那么您将无法使用它,您应该改用 have_http_status。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多