How can I use block syntax with Minitest? - ruby

In Rails we can use Minitest with this syntax:
test "true" do
assert true
end
When I try using this syntax in my gem I get an ArgumentError (wrong number of arguments).
To be clear, I don't care for the rspec syntax (I prefer assert over describe blocks). I only want to write test "foo" do instead of def test_foo.
How can I do this?
My test_helper.rb is almost empty:
gem 'minitest'
require 'minitest/autorun'
require 'foo/bar'

This is an ActiveSupport thing as far as I know, not a Minitest thing
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/testing/declarative.rb
A quick thing you could do is require 'minitest/spec' and alias it as test

Related

Ruby Gem: Uninitialized constant FactoryBot

Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
and in spec_helper.rb:
require 'support/factory_bot'
When I try to run the spec rake task, I get this error:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
What am I missing? This used to work fine when I was using the old factory_girl gem, but it has broken with the rename to factory_bot. Thanks!!
Doh. Silly mistake here, running bundle exec rake spec instead of rake spec solved it.
Also had to add require 'factory_bot' to the top of support/factory_bot.rb
Overview just in case you are doing this from scratch
installation rspec details here (basically add gem to Gemfile then run bundle install)
initialize RSPEC in your rails project rails g rspec:install
create new file your spec/support/factory_bot.rb add the following base code:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
add reference on spec/rails_helper.rb
require 'support/factory_bot'
as well as remove any fixture unused reference like this one
config.use_transactional_fixtures = true
That should be it!, finally run any spec file you want inside rspec default folders
e.g.:
spec/features/my_action_spec.rb
spec/models/my_model_spec.rb
spec/task/my_task_spec.rb
or run them all and check depending on your setup
rspec
rails rspec
bundle exec rspec
hope this helps someone with the whole RSPEC + FactoryBot Gem installation process
I had this problem too, remove the
require 'support/factory_bot'
There's a line on rails_helper, just uncomment it:
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
I had encountered and issue similar to this. I worked around it by removing the default testing suite ( MiniTest ). When you create a rails application and intend on using rspec and factory_bot, use the code below in the command line:
rails new myapp -T
Hope this helps xP
In my case I had to put those lines below 'require "rspec/rails" in file:
spec/rails_helper.rb:
like:
require "rspec/rails"
require_relative "support/factory_bot"
require_relative "support/chrome"

NoMethodError: undefined method `assert_true'

NoMethodError: undefined method `assert_true'
Am getting above error while running tests using test-unit in ruby. test-unit gem and rake versions are below,
test-unit (2.5.5)
rake (10.1.0)
Sample test file:-
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
How to solve this ?
I solved the problem by using following way. No need to change assert statements.
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
assert_true does not appear to be in the list of available assertions for test-unit. Try using assert. Reference: http://ruby-doc.org/stdlib-2.0.0/libdoc/test/unit/rdoc/Test/Unit/Assertions.html
Since 1.9.2, test/unit is a wrapper around minitest, implemented directly in the ruby source code. The assert_true method does not exist in the new implementation, just use assert instead, as Simon Brahan already suggested. So the gem source you were looking at is no longer in use. The now relevant documentation is here.

"require if" in Ruby

Here is what I have in my rack app
#rb file
require 'pry'
class .....
#GemFile
group :development do
gem "pry"
gem "pry-nav"
end
Of course, in production it causes an error. How do make a kind of "require if"?
require 'pry' if ENV['RACK_ENV'] == 'development'
May be you can embed it inside a if block
according to docs Sinatra provides a environment variable
http://www.sinatrarb.com/intro#Environments
if development?
require 'pry'
end
wherever you need to use it.
this may not be the exact solution you may be looking for just a wild guess
I suggest to write such method in Object or Kernel in your app:
def require_pry
require 'pry' if ENV['RACK_ENV'] == 'development'
end
After that you can call require_pry if you need it in your code. But I have doubts why it cannot be handled by Bundler, Bundle.require will require all gems needed for environment.

How to color unit tests with lib minitest or Test:Unit?

I would like to have unit tests output color in my dev environment. However, I can't make it work on Linux (Debian and Ubuntu). When I include the following libs:
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/pride'
I get:
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `<top (required)>': undefined method `output' for MiniTest::Unit:Class (NoMethodError)
caused by the code:
MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output)
I have seen a working Rspec variant. Unfortunately, my Ruby knowledge is not enough to see differences.
Give turn a whirl.
Add this to your Gemfile:
group :test do
gem 'turn', :require => false
end
step 1 : use the latest version of the gem (I think it will be fixed in Ruby 1.9.3)
gem install minitest
step 2 : require "minitest/pride" on the command line, not in your code
ruby -rminitest/pride your_ruby_script.rb
.. and in your code simply require 'minitest/autorun'
require 'minitest/autorun'
If you use Rubymine, just add
-rminitest
in the default configuration of the tests.
=> the configuration would like
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -rminitest/pride
simply add these lines to your test_helper.rb file after require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
in your gemfile add
gem 'minitest-reporters', '~> 1.0.7'
this will get your rake test to be in red and green, but it also brings up the backtrace. to get rid of all those extra backtrace logs add this to your gemfile then bundle:
gem 'mini_backtrace'
then in config/initializers/backtrace_silencers.rb add this line to cut all the extra rvm stuff
Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }
hope that works for you, let me know if you need more details.
See https://github.com/tenderlove/purdytest/issues/1. It seems to be a known bug with the minitest version shipped with 1.9.2. For the others, adding
gem "minitest"
at the very top of your file did the trick.
I currently use purdytest with 1.9.2
Try look at this post about using turn gem for nice looking and configurable output for minitest.
http://rawonrails.blogspot.com/2012/01/better-minitest-output-with-turn-gem.html
$ gem install mynyml-redgreen --source http://gemcutter.org
# in your test file
require 'redgreen'
redgreen and turn work nicely in conjunction with each other, by the way

Pathname.rb error on running a minitest testcase

I'm running Ruby 1.8.6.
I installed the minitest 1.3.1 gem, which is the new defacto replacement for the Test::Unit framework in Ruby 1.9 The API is supposed to be the same.
I wrote a small test to get things rolling:
require 'rubygems'
gem 'minitest'
require 'minitest/unit'
MiniTest::Unit.autorun
class CategoryMiniTest < MiniTest::Unit::TestCase
def test_twoCategoriesCannotHaveSameName
assert_equals(2,2)
end
end
Which leads to:
>ruby test\unit\category_mini_test.rb
l:/ruby_home/lib/ruby/1.8/pathname.rb:709:in `relative_path_from': different prefix: "l:/" and "L:/Gishu/Ruby/Rails/ShowMeTheMoney" (ArgumentError)
from l:/ruby_home/lib/ruby/gems/1.8/gems/minitest-1.3.1/lib/minitest/unit.rb:17
What gives?
I can't see anything wrong with your code. It looks almost exactly the same as the Ruby 1.8.6 & MiniTest example in my blog post: Test::Unit and MiniTest with different Ruby versions.
So I wonder if it is:
something to do with your environment,
something to do with how you are running the test, or
a bug in MiniTest.
Looking at the error message, I wonder whether the problem is with case-sensitivity - the upper-case and lower-case L drive letters may not match.

Resources