我正在尝试进行简单的验证,主要的问题是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)
我可以通过be_valid使其通过,但已弃用。
如果我使用have_at_least,则错误发生在errors_on上
请参阅此拉取请求。have(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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句