shoulda-matchers
shoulda-matchers copied to clipboard
shoulda-matcher could not found test class name when work with rails 6 minitest
I'm using rails 6 and with most of ActionController matchers is not working.
Tests Controller
require 'test_helper'
class GradesControllerTest < ActionDispatch::IntegrationTest
extend Shoulda::Matchers::ActionController
context 'GET #index' do
setup { get :index }
should use_before_action(:set_course)
should render_template('index')
should respond_with(:success)
end
Errors
/home/juanfra/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb:251: warning: BigDecimal.new is deprecated; use BigDecimal() method instead.
Run options: --seed 61573
# Running:
.E
Error:
GradesControllerTest#test_: GET #index should render template index. :
NoMethodError: undefined method `body' for nil:NilClass
rails test /home/xxxx/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/shoulda-context-1.2.2/lib/shoulda/context/context.rb:346
E
Error:
GradesControllerTest#test_: GET #index should respond with 200. :
NoMethodError: undefined method `response_code' for nil:NilClass
rails test /home/xxxx/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/shoulda-context-1.2.2/lib/shoulda/context/context.rb:346
Gemfile
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
gem 'simplecov', require: false, group: :test
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'rails-controller-testing'
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers', '~> 2.0'
end
I think this is someway relate to this issues #1229, #1217, #1158.
I have add this line to make it work for some of the matchers as you suggest in #1158 :
extend Shoulda::Matchers::ActionController
But in the matchers that need the className is not working at all.
We're a bit behind in supporting Minitest at the moment. I don't think we even test that you can use controller matchers in an ActionDispatch::IntegrationTest (although in theory they should work).
I know the README advises staying on shoulda-matchers 2.x, but given that we are to 4.x now, what if you try upgrading? There's a pre-release version of shoulda-context out that should fix any compatibility issues. Instead of using this in your gemfile,
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers', '~> 2.0'
try:
gem 'shoulda-context', '2.0.0.rc2'
gem 'shoulda-matchers', '~> 4.0'
Then add the following to your test_helper
:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :minitest
with.library :rails
end
end
I just tried what you suggests, it works just at before and I don't need now the
extend Shoulda::Matchers::ActionController
But its still failing on the same
Error:
GradesControllerTest#test_: GET #index should render template index. :
NoMethodError: undefined method `body' for nil:NilClass
rails test /home/xxxx/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/shoulda-context-2.0.0.rc2/lib/shoulda/context/context.rb:62
E
Error:
GradesControllerTest#test_: GET #index should respond with 200. :
NoMethodError: undefined method `response_code' for nil:NilClass
rails test /home/xxxx/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/shoulda-context-2.0.0.rc2/lib/shoulda/context/context.rb:62
Hmm, alright. I don't have any immediate insight as to why this could be happening, but I'll look into this to see if there is a workaround.
Great thanks!
@mcmire Hi there, not sure if this issue is still being looked at, but I'm experiencing similar issues.
After installing shoulda-matchers 4.4.1 in Rails 6.0.3 (ruby 2.7), I get undefined method 'context' for UserTest:Class (NoMethodError)
.
gemfile
group :test do
gem "shoulda-matchers", "~> 4.0"
end
test_helper
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require "minitest/mock"
class ActiveSupport::TestCase
include FactoryBot::Syntax::Methods
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
# fixtures :none
# Add more helper methods to be used by all tests here...
def switch_account(account)
patch "/accounts/#{account.id}/switch"
end
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :minitest
with.library :rails
end
end
test
require "test_helper"
class UserTest < ActiveSupport::TestCase
context 'associations' do
should have_many(:accounts)
end
end
backtrace
context
is a part of shoulda-context. We updated the shoulda
gem a little while ago to support Rails 6, so you should be able to replace shoulda-matchers
with shoulda
and be able to use context
.
@mcmire With or without context, it seems matcher methods are not defined appropriately.
Example per the docs:
Okay, sanity check. Which version of shoulda-context is being brought in?
I don't believe any version of shoulda-context
, I'm only trying to include shoulda-matchers
in my minitest suite.
Unsure if I missed an installation step?

Right. So shoulda-matchers
does not provide support for using should
like that in Minitest::Test or ActiveSupport::Test subclasses. It merely provides the matchers that you can use. The should
and context
functionality is provided by shoulda-context
. You will need to include that gem as well in order to use that. Or, simply add shoulda
(instead of shoulda-matchers
) in your Gemfile and you will get both.
Thanks for that clarification. I've installed shoulda
and re-run the tests, nothing blew up but I'm seeing zero's across the board; I'd expect 1 run and 1 assertion. Is there additional setup I'm missing? I don't see additional installation steps.
I get the same results if I use context as well.

EDIT -- happy to open this as a separate issue
The issue reported above is a false negative on my part due to my test running shortcut -- I'm no longer having issues, thanks!
@mrjonesbot Ah, no worries, glad you figured it out!