Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jfcampos1 opened this issue Oct 16, 2019 · 13 comments

Comments

@jfcampos1
Copy link

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.

@mcmire
Copy link
Collaborator

mcmire commented Oct 16, 2019

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

@jfcampos1
Copy link
Author

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

@mcmire
Copy link
Collaborator

mcmire commented Oct 17, 2019

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.

@jfcampos1
Copy link
Author

Great thanks!

@mrjonesbot
Copy link

@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

@mcmire
Copy link
Collaborator

mcmire commented Oct 30, 2020

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.

@mrjonesbot
Copy link

mrjonesbot commented Oct 30, 2020

@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

@mcmire
Copy link
Collaborator

mcmire commented Oct 30, 2020

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

@mrjonesbot
Copy link

mrjonesbot commented Oct 30, 2020

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

@mcmire
Copy link
Collaborator

mcmire commented Oct 30, 2020

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.

@mrjonesbot
Copy link

mrjonesbot commented Oct 30, 2020

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
Copy link

The issue reported above is a false negative on my part due to my test running shortcut -- I'm no longer having issues, thanks!

@mcmire
Copy link
Collaborator

mcmire commented Nov 2, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants