NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers)
NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers)
我正在尝试设置 Rspec 和 Shoulda-Matchers,但由于某种原因我收到此错误:
NoMethodError:
undefined method `validate_presence_of' for #RSpec::ExampleGroups::AdCampaign::Validations:0x000000062a2b90>
Failure/Error: it { should have_many :advertisements }
expected # to respond to has_many?
NoMethodError:
undefined method `belong_to' for #RSpec::ExampleGroups::AdCampaign::Associations:0x0000000686d8e0>
似乎我尝试了 Whosebug 和 github 问题的所有答案,但没有任何帮助。
也许你能帮我找出我做错了什么?
这是我的 rails_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'shoulda/matchers'
require 'spec_helper'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
这是我的 spec_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'database_cleaner'
require 'capybara/rspec'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include FactoryGirl::Syntax::Methods
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
# config.include Shoulda::Matchers::ActiveRecord, type: :model
# config.include Devise::TestHelpers, type: :controller
config.order = 'random'
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
这是我测试的规范文件:
require 'rails_helper'
RSpec.describe AdCampaign, type: :model do
describe 'validations' do
it { should validate_presence_of :shop }
it { should validate_presence_of :description }
end
describe 'associations' do
it { should belong_to :shop }
it { should have_many :advertisements }
it { should have_one :recipient_list }
end
end
这是我要测试的模型:
class AdCampaign < ActiveRecord::Base
belongs_to :shop
has_many :advertisements
has_one :recipient_list
validates :shop, :description, presence: true
end
我试过将 Shoulda::Matchers.configure do...
放在 rails_helper.rb
和 spec_helper.rb
中。
在我的 gem 文件中,我有 shoulda-matchers
gem 这样的:
group :development, :test do
gem 'shoulda-matchers', require: false
end
你能帮我设置 shoulda-matchers 和 rspec 吗?我在这里做错了什么?
我遇到了类似的问题!我帮助了这个:
# Gemfile
group :test do
gem "shoulda-matchers", require: false
end
这是要加到最前面的rspec_helper.rb
require 'shoulda-matchers'
require "bundler/setup"
::Bundler.require(:default, :test)
::Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
#with.test_framework :minitest
#with.test_framework :minitest_4
#with.test_framework :test_unit
# Choose one or more libraries:
with.library :active_record
with.library :active_model
#with.library :action_controller
# Or, choose the following (which implies all of the above):
#with.library :rails
end
end
我正在尝试设置 Rspec 和 Shoulda-Matchers,但由于某种原因我收到此错误:
NoMethodError: undefined method `validate_presence_of' for #RSpec::ExampleGroups::AdCampaign::Validations:0x000000062a2b90>
Failure/Error: it { should have_many :advertisements } expected # to respond to
has_many?
NoMethodError: undefined method `belong_to' for #RSpec::ExampleGroups::AdCampaign::Associations:0x0000000686d8e0>
似乎我尝试了 Whosebug 和 github 问题的所有答案,但没有任何帮助。
也许你能帮我找出我做错了什么?
这是我的 rails_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'shoulda/matchers'
require 'spec_helper'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
这是我的 spec_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'database_cleaner'
require 'capybara/rspec'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include FactoryGirl::Syntax::Methods
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
# config.include Shoulda::Matchers::ActiveRecord, type: :model
# config.include Devise::TestHelpers, type: :controller
config.order = 'random'
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
这是我测试的规范文件:
require 'rails_helper'
RSpec.describe AdCampaign, type: :model do
describe 'validations' do
it { should validate_presence_of :shop }
it { should validate_presence_of :description }
end
describe 'associations' do
it { should belong_to :shop }
it { should have_many :advertisements }
it { should have_one :recipient_list }
end
end
这是我要测试的模型:
class AdCampaign < ActiveRecord::Base
belongs_to :shop
has_many :advertisements
has_one :recipient_list
validates :shop, :description, presence: true
end
我试过将 Shoulda::Matchers.configure do...
放在 rails_helper.rb
和 spec_helper.rb
中。
在我的 gem 文件中,我有 shoulda-matchers
gem 这样的:
group :development, :test do
gem 'shoulda-matchers', require: false
end
你能帮我设置 shoulda-matchers 和 rspec 吗?我在这里做错了什么?
我遇到了类似的问题!我帮助了这个:
# Gemfile
group :test do
gem "shoulda-matchers", require: false
end
这是要加到最前面的rspec_helper.rb
require 'shoulda-matchers'
require "bundler/setup"
::Bundler.require(:default, :test)
::Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
#with.test_framework :minitest
#with.test_framework :minitest_4
#with.test_framework :test_unit
# Choose one or more libraries:
with.library :active_record
with.library :active_model
#with.library :action_controller
# Or, choose the following (which implies all of the above):
#with.library :rails
end
end