shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

shoulda-matcher could not found test class name when work with rails 6 minitest

Open jfcampos1 opened this issue 5 years ago • 13 comments

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.

jfcampos1 avatar Oct 16 '19 20:10 jfcampos1

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

mcmire avatar Oct 16 '19 22:10 mcmire

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

jfcampos1 avatar Oct 17 '19 12:10 jfcampos1

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.

mcmire avatar Oct 17 '19 22:10 mcmire

Great thanks!

jfcampos1 avatar Oct 18 '19 05:10 jfcampos1

@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

Screen Shot 2020-10-30 at 12 37 52 AM

mrjonesbot avatar Oct 30 '20 05:10 mrjonesbot

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 avatar Oct 30 '20 15:10 mcmire

@mcmire With or without context, it seems matcher methods are not defined appropriately.

Example per the docs: Screen Shot 2020-10-30 at 10 29 39 AM

mrjonesbot avatar Oct 30 '20 15:10 mrjonesbot

Okay, sanity check. Which version of shoulda-context is being brought in?

mcmire avatar Oct 30 '20 15:10 mcmire

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?

Screen Shot 2020-10-30 at 10 37 08 AM

mrjonesbot avatar Oct 30 '20 15:10 mrjonesbot

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.

mcmire avatar Oct 30 '20 16:10 mcmire

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.

Screen Shot 2020-10-30 at 11 56 29 AM

EDIT -- happy to open this as a separate issue

mrjonesbot avatar Oct 30 '20 16:10 mrjonesbot

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 avatar Nov 01 '20 15:11 mrjonesbot

@mrjonesbot Ah, no worries, glad you figured it out!

mcmire avatar Nov 02 '20 15:11 mcmire