gpt4 book ai didi

ruby-on-rails - 应该匹配错误:# 的未定义方法 `validate_presence_of'

转载 作者:数据小太阳 更新时间:2023-10-29 08:14:35 24 4
gpt4 key购买 nike

我正在学习 TDD 并尝试使用 shoulda_matchers 来帮助我进行测试,但我遇到了一个非常奇怪的错误。这是我的测试:

规范/模型/idea_spec.rb

require 'rails_helper'

describe Idea do

context 'Validations' do
describe 'title' do
it { should validate_presence_of(:title) }
end
end
end

测试错误说:

Idea Validations title 
Failure/Error: it { should validate_presence_of(:title) }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f056f9fcdf8>
# ./spec/models/idea_spec.rb:7:in `block (4 levels) in <top (required)>'

根据 gem 说明,require 'shoulda/matchers' 位于我的 rails_helper 文件的顶部。

我正在使用的 gem :

group :development, :test do
gem 'spring'
gem 'dotenv-rails'
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
end

group :test do
gem 'webmock', '~> 1.20.4'
gem 'shoulda-matchers', require: false
end
  1. ruby '2.1.2'
  2. rails ', '4.1.9'

最佳答案

Shoulda-Matchers 不再自动将自己安装到您的测试框架中。您需要将此添加到您的 rails_helper:

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

参见 this GitHub issue , 阅读 note on shoulda-matchers configuration ,并阅读此 blog post by maintainers .

关于ruby-on-rails - 应该匹配错误:#<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f3f88fa7548> 的未定义方法 `validate_presence_of',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com