I tried to use shoulda-matchers to test the association between models. However, it always show Error:
TakingTest#test_belongs_to:
NoMethodError: undefined method belong_to' for #<TakingTest:0x00007fc14b8e64a8>
test/models/taking_test.rb:8:in
block in '
I checked other answer, most of them are at least 4 years ago. Does it still work with rails 6.0?
ruby '2.6.5'
rails', '~> 6.0.2'
gem file
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers'
end
spec/rails_helper.rb:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
test/models/taking_test.rb
class TakingTest < ActiveSupport::TestCase
test "belongs to" do
should belong_to(:students)
end
end
Having both a
spec
directory and atest
directory could be causing your problem. IMO, there should only ever bespec
ortest
for a every project, never both.Usually, test files begin with an include for the
test_helper.rb
file.Instead of a
test_helper
, you've got aspec_helper
.Try moving the Shoulda initializer from
spec/spec_helper.rb
totest/test_helper.rb