【问题标题】:Rspec issue with method方法的Rspec问题
【发布时间】:2014-06-19 00:44:45
【问题描述】:

我正在尝试做一个简单的验证,主要问题是 Rails 版本(我一直在使用 RSpec 和 Rails 4),因为我在 Rails 3.2 和 Ruby 1.9.3 中使用它。

这是我模型中的代码

expect(section).to have(1).errors_on(:name)

并返回此错误

1) Admin::Section is invalid without name
 Failure/Error: expect(section).to have(1).errors_on(:name)
 NoMethodError:
   undefined method `have' for #<RSpec::ExampleGroups::AdminSection:0x007f81cf6e72d0>

我的宝石清单是这样的

* rspec-core (3.0.1)
* rspec-expectations (3.0.1)
* rspec-mocks (3.0.1)
* rspec-rails (3.0.1)
* rspec-support (3.0.0)

我可以通过 should be_valid 让它通过,但它已被弃用。

如果我使用 have_at_least,则错误出现在 errors_on

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec ruby-on-rails-3.2


    【解决方案1】:

    看到这个pull requesthave(n) 被提取 rspec-collection_matchers gem

    您可以包含它或仅使用以下之一:

    expect(section.error_on(:name)size).to eq(1)
    expect(section.error_on(:name)).to be_present
    expect(section).to have_validation_error("your error message").on(:name)
    

    【讨论】:

    • 同样的错误。 用于的未定义方法'error_on'和用于的未定义方法'on'
    • 我添加了 gem "rspec-collection_matchers",所以它按照我习惯的方式工作,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-02-09
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多