my tests don't run with minitest - ruby-on-rails

I'm totally new in testing and after two years of rails development, it has to change!
Therefore, I'm trying to install minitest on a rails 4 application.
I did the following:
updated my gemfile:
group :test do
gem 'factory_girl'
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'minitest-colorize'
gem 'minitest-focus'
gem "shoulda-matchers"
gem "guard-rspec"
end
created a helper in test/test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
#require "minitest/rails"
require "minitest/spec"
require 'minitest/focus'
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
require "minitest/rails/capybara"
# Uncomment for awesome colorful output
require "minitest/pride"
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
ActiveRecord::Migration.check_pending!
fixtures :all
extend MiniTest::Spec::DSL
#use ActiveSupport::TestCase when describing an ActiveRecord model
register_spec_type self do |desc|
desc < ActiveRecord::Base if desc.is_a? Class
end
# Add more helper methods to be used by all tests here...
def self.prepare
# Add code that needs to be executed before test suite start
end
prepare
def setup
# Add code that need to be executed before each test
end
def teardown
# Add code that need to be executed after each test
end
end
generated a scaffold to have some "test" testfiles, like this one:
models/project_test.rb
require "test_helper"
describe Project do
before do
#project = Project.new
end
it "must be valid" do
#project.valid?.must_equal true
end
end
when I type rake minitest:models in terminal, this is what I get:
Run options: --seed 40421
# Running tests:
Fabulous tests in 0.049207s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Apparently, minitest runs but my tests are not executed. What's wrong?
Thanks!!!

The issue was due to a conflict with rspec which was added as a dependency by guard-rspec.
many thanks to #blowmage!

Related

How to prevent RSpec from running specs twice in Rails plugin with dummy app?

I'm writing a Rails extension. To test it, I use RSpec. In order to make models to test the plugin on, I use a pregenerated dummy app:
rails plugin new yaffle --dummy-path=spec/dummy --skip-test --full
I have a few tests. When I call them, they run twice.
> # I have 4 tests in app total
> rspec
> 8 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb
> 4 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb:4
> 2 examples, 0 failures
This is how my files look like:
# lib/yaffle.rb
Gem.find_files('yaffle/**/*.rb').each { |path| require path }
module Yaffle
# Your code goes here...
end
# spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
RSpec.configure do |config|
config.example_status_persistence_file_path = '.rspec_status'
config.disable_monkey_patching!
end
# spec/dummy/config/environment.rb
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
# spec/yaffle/active_record/acts_as_yaffle_spec.rb
require "spec_helper"
RSpec.describe "Acts as yaffle" do
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal "last_squawk", Hickwall.yaffle_text_field
end
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal "last_tweet", Wickwall.yaffle_text_field
end
end
# spec/dummy/config/application.rb
require_relative 'boot'
require "rails/all"
Bundler.require(*Rails.groups)
require "yaffle"
module Dummy
class Application < Rails::Application
config.load_defaults 6.0
end
end
Also, I noticed that only files with require "spec_helper" are duplicated
So, what am I doing wrong? And, if it's a bug, how to work around it?
The cause
It was caused by this line:
Gem.find_files('yaffle/**/*.rb').each { |path| require path }
Apparently, Gem.find_files gets all files in gem directory that match that pattern - not only the files relative to gem root. So, yaffle/**/*.rb means both files in <GEM_ROOT>/lib/yaffle/... and <GEM_ROOT>/spec/lib/yaffle/....
https://apidock.com/ruby/v1_9_3_392/Gem/find_files/class
Fix
I fixed it by requiring all files explicitly:
require 'lib/yaffle/active_record/acts_as_yaffle'
require 'lib/yaffle/active_record/has_fleas'
It's also possible to just require all files from that directory:
Dir["lib/yaffle/active_record/**/*.rb"].each {|file| require file }

Minitest console output does not show colors

I installed ansicon to make ansi colorized console output possible for minitest test feedback on windows.
I am running minitest with the minitest-reporters gem to format the output, yet whatever I do I can't get the output to show colors (all text is black).
Here's my test_helper.rb file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new( { :color => true } )
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
Does anyone know this problem?
Reference: https://github.com/kern/minitest-reporters
I see the post is from 2015. 4 years passed since then.
Anyway, let me share how it worked for me yesterday.
My environment:
Ubuntu 18.04
rails 6.0.1
ruby 2.6.5
1) Gemfile:
gem 'minitest-reporters'
2) bundle install
3) test_helper.rb:
require "minitest/reporters"
Minitest::Reporters.use!
4) rake

Mintest Rails4: ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint

I'm trying test my rails 4 application with MiniTest. I've some seed data, to get app setup. Once seed data is loaded in test database, I try to run test.
test_seed.rake
namespace :db do
namespace :test do
desc "Load seed data before running tests"
task :prepare => :environment do
puts "\nLoading Seed data.....\n\n"
Rake::Task["db:seed"].invoke
puts "\n....Seed data loaded!\n\n"
end
end
end
Having done that, I do
rake db:test:prepare
Then when I simply put 'rake' command(as of now I do not have any testcase written)
test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
require "minitest/rails/capybara"
# Uncomment for awesome colorful output
require "minitest/pride"
# Code coverage, refer https://github.com/colszowka/simplecov
require 'simplecov'
SimpleCov.start
# Create customizable MiniTest output formats
require "minitest/reporters"
Minitest::Reporters.use!(
Minitest::Reporters::SpecReporter.new,
ENV,
Minitest.backtrace_filter
)
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
include FactoryGirl::Syntax::Methods
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
extend MiniTest::Spec::DSL
end
class ActionDispatch::IntegrationTest
extend MiniTest::Spec::DSL
include Capybara::DSL
end
Gemfile:
group :test do
# MiniTest
gem 'minitest-spec-rails', '5.2.0'
gem 'minitest-rails-capybara', '2.1.1'
gem 'minitest-reporters', '1.0.5'
gem 'guard-minitest', '2.3.1'
gem 'launchy'
gem 'factory_girl_rails', "~> 4.2.1"
gem 'faker', '1.4.3'
gem 'simplecov', '0.9.2', require: false
end
Existing test, role_test.rb:
require 'test_helper'
class RoleTest < ActiveSupport::TestCase
test "nothing" do
assert true
# role_names = Role.where(name: 'superAdmin').collect(&:name)
# assert_includes role_names, 'SuperAdmin', 'it should include role superAdmin'
end
It fails with following error:
RoleTest
test_0001_nothing ERROR (0.02s)
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint
DETAIL: Failing row contains (980190962, null, 2015-03-31 19:51:59, 2015-03-31 19:51:59, null, null, null, null, null, null, null).
: INSERT INTO "companies" ("created_at", "updated_at", "id") VALUES ('2015-03-31 19:51:59', '2015-03-31 19:51:59', 980190962)
Fabulous run in 0.05302s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
Can anyone help me on this, why at all I'm getting this error? And if possible, suggestion to fix it.
Thanks

Why doesn't Minitest run any tests?

I've used Minitest on several projects (both strictly Ruby and Rails) and haven't had much of an issue. I just set up a Rails 4 project with Minitest and can't get the tests to run for some reason. This must be a stupid mistake on my part, but I can't see it.
The "error", "minitest running 0 tests", happens whether I use rake or ruby Itest.
I should also say that I know the test file is being recognized because if I take out the describe block, and just have it should..., I get a NoMethodError - undefined method "it" for CalendarMakerTest:Class.
#test
require 'test_helper'
class CalendarMakerTest < ActionView::TestCase
describe "subject" do
it "does something" do
assert_equal false
end
end
end
#test_helper
require 'minitest/autorun'
require 'minitest/spec'
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
end
$ruby -Itest test/helpers/calendar_maker_test.rb
Run options: --seed 33758
# Running tests:
Finished tests in 0.035027s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
You can remove the line
class CalendarMakerTest < ActionView::TestCase
Along with the corresponding end statement. You will have better luck.
You are mixing up the ways to use Minitest with the spec and the autotest methodology.
You also don't technically need require minitest/spec when you require minitest/autorun.

How to run single test class using minitest in ROR

My Gemfile looks like this:-
group :test do
# Pretty printed test output
gem 'capybara'#,'1.1.2'
gem 'cucumber-rails','1.2.1'
gem 'cucumber','1.1.4'
gem 'rspec-rails','2.8.1'
gem 'rspec-cells','0.1.2'
gem "factory_girl_rails"
gem "guard-rspec"
gem "minitest"
gem 'headless'
gem 'minitest-rails'
gem 'minitest-rails-capybara'
end
minitest_helper.rb looks like :-
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
class ControllerTest < MiniTest::Spec
include Rails.application.routes.url_helpers
include Capybara::DSL
register_spec_type(/integration$/, self)
end
And my products_controller_test.rb looks like this:-
require "minitest_helper"
describe "Products Controller" do
it "shows product's name" do
uname="Glasses"
product1 = Product.create!(:name => uname, :description => uname, :no_of_items => 3,:fee_percentage => 4)
visit products_path
page.text.must_include "Glasses"
end
end
BUT..after executing ruby -Itest test/controllers/products_controller_test.rb
I get no error,no indication to show that this test class has been loaded :-
ruby -Itest test/controllers/products_controller_test.rb
:public is no longer used to avoid overloading Module#public, use :public_folder instead
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/resque-1.19.0/lib/resque/server.rb:12:in `<class:Server>'
Loaded suite test/controllers/products_controller_test
Started
Finished in 0.004953 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
its first time i am using Minitest...
Your Gemfile is a little heavy... If you remove all the RSpec references, you'll run just fine.
(The "describe" and "it" methods are being usurped by rspec)
Remove:
gem 'rspec-rails','2.8.1'
gem 'rspec-cells','0.1.2'

Resources