Ruby: mnitest-rails + mocha - ruby-on-rails

The instructions for using the mocha mocking library with minitest don't work, and I don't know why... minitest 4.2.0, minitest-rails 0.3, mocha 0.12.4. Per the instructions, at the bottom of Gemfile I have
group :test, :development do
gem 'mocha', :require => false
end
which is supposed to turn off auto-requiring. And then at the bottom of the test helper files, I have require 'mocha' - the instructions say require 'mocha/setup', which doesn't even exist. With these two bits of code in, all the tests that run fine without mocha in the gemfile all fail with "undefined method 'run' blah blah blah". Anyone see what I'm missing here?

Sigh, apparently those messages are indicative of having failed to install the mocha rails plugin. That was the problem here.

Related

minitest_plugin.rb:9 getting wrong number of arguments

~/Sites/sample_app$ rails test
Running via Spring preloader in process 24338
Run options: --seed 58780
Running:
..
Finished in 0.292172s, 6.8453 runs/s, 6.8453 assertions/s.
/var/lib/gems/2.3.0/gems/railties-5.1.0/lib/rails/test_unit/minitest_plugin.rb:9:in `aggregated_results': wrong number of arguments (given 1, expected 0) (ArgumentError)
I don't understand why I am getting this error. I Can't seem to find anyone with this specific error. I'm following the tutorial https://www.railstutorial.org/book/static_pages. This error follows the rails test command. Running Ubuntu and rails 5.1 if that helps. I'm not passing any arguments so I don't understand why I am getting this error.
My test file looks like :
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help_url
assert_response :success
end
This is actually a bug in rails, revealed by minitest update to 5.10.2 as said here and it has already been fixed here.
As the pull request is only 10 hours old (at the time of writing), it has not yet been released, although it's already merged.
In the mean time, you can specify in your Gemfile:
gem 'minitest', '~> 5.10', '!= 5.10.2'
Edit
Don't forget to bundle update minitest
Hey I'm doing this exact tutorial and followed the top solution and it fixed my issue, specifically (for us total noobs) I did this to my Gemfile
group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
gem 'minitest', '~> 5.10', '!= 5.10.2' # add this here to fix error
end
It turns out that in my test/test_helper.rb I needed a line of code that was missing. I added this before "class ActiveSupport::TestCase".
Minitest::Reporters.use!
This gave me a passing result for my test with no strange argument error. Hope this helps someone for the future!
Problem is in minitest 5.10.2. Downgrading to 5.10.1 fixes it.
Add this line in test group in Gemfile
gem 'minitest', '5.10.1'
Then run following commands
bundle update minitest
rails test
The issue is resolved
I also encountered this error. I tried using the top voted solution, but rails told me that bundle has locked minitest to 5.10.2. Instead, I just did a gem update minitest and my tests ran without the weird error.

Where is rails_helper.rb file?

I am currently following this tutorial to begin testing rspec with capybara and selenium. I included the necessary gems and ran a bundle install, but cant seem to find the rails_helper.rb to continue with the tutorial.
This is on rails 4.2.4.
Thanks,
Otterman
rails_helper.rb in generated when you run rails g rspec:install. Make sure you are using a relatively recent version of rspec-rails as versions before 3.0 only the spec_helper.rb is used configure the test suite (well by default at least).
The spec_helper.rb file is used to set up RSpec itself and rails_helper.rb sets up the rails stack.
You should be able to find the file in ../spec/rails_helper
As mentioned, I was missing
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
in my gemfile.
After which I ran
rails generate rspec:install

Problems with Ruby versions

I am learning Ruby and have to do some homework, but have problem with executing code. I searched this forum and Google and found that this could be problem with different Ruby versions but I don't know how to fix this. I've tried to execute:
cucumber features/filter_movie_list.feature
and got this:
And I should not see 'G' #
features/step_definitions/movie_steps.rb:32
no such file to load -- rspec/matchers/built_in/has (LoadError)
./features/step_definitions/movie_steps.rb:34:in /^(?:|I )should not see '([^']*)'$/'
features/filter_movie_list.feature:37:inAnd I should not see 'G''
Can someone help me to solve this problem or give me link to solution?
EDIT:
movie_steps.rb relevant for this taks is:
Then /^(?:|I )should not see '([^']*)'$/ do |text|
if page.respond_to? :should
page.find('#movies').should have_no_content(text)
else
assert page.find('#movies').has_no_content?(text)
end
end
The error message says that Cucumber can't find RSpec.
To solve it, edit your Gemfile and add these and that should solve it.
group :test do
gem 'rspec' # Behavior Driven Development (BDD) for Ruby
gem 'rspec-core' # RSpec runner and example groups.
gem 'rspec-expectations' # RSpec matchers for should and should_not.
gem 'rspec-mocks' # RSpec test double framework with stubbing and mocking.
gem 'rspec-rails' # RSpec version 2.x for Rails version 3.x.
end
If that does solve it, you can probably remove all those lines except the first one (the 'rspec' one) because that line should take care of the others.

Autotest ruby on rails with rspec

I'm using autotest with ruby on rails. I have passing 2 passing tests when I run. rspec spec/; however, when I try to use autotest this is the output:
matt#matt-laptop:~/sample_app$ autotest
loading autotest/rails_rspec2
style: RailsRspec2
matt#matt-laptop:~/sample_app$
I get no output about the results of the tests. The same thing works with bundle exec autotest. I saw a post recommending autospec but that command is deprecated with rspec2. My Gemfile is
source 'http://rubygems.org'
gem 'rails', '3.0.5'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'
group :development do
gem 'rspec-rails', '2.5.0'
gem 'autotest','4.4.4'
end
group :test do
gem 'rspec', '2.5.0'
gem 'webrat', '0.7.1'
gem 'autotest', '4.4.4'
gem 'redgreen', '1.2.2'
end
I have tried putting the .autotest config file in the root directory of my project as well as the home directory and neither makes a difference on the output. My .autotest file looks like this
#!/bin/ruby
require 'autotest/timestamp'
module Autotest::GnomeNotify
def self.notify title, msg, img
system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
end
Autotest.add_hook :ran_command do |at|
image_root = "~/.autotest_images"
results = [at.results].flatten.join("\n")
results.gsub!(/\\e\[\d+m/,'')
output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)
full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
if output
if failures > 0
notify "FAIL", "#{output}", "#{image_root}/fail.png"
elsif pending > 0
notify "Pending", "#{output}", "#{image_root}/pending.png"
else
notify "Pass", "#{output}", "#{image_root}/pass.png"
end
end
end
end
I've also checked that libnotify-bin is installed and functioning.
To get verbose results from rspec, create a .rspec file in your root project folder and write :
--format documentation
If i may, allow me to suggest watchr instead of autotest (with spork as well). Very easy to set up and very effective.
Take a look at
http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RubyInside+%28Ruby+Inside%29
if you like.
I've had much the same problem with Autotest. I'm not certain, but I believe it may be very finicky about dependency versions. I'm a bit of a Rails noob, but since I so recently shared your troubles, let me show you what I did to fix the problem:
I was able to get it working by getting rid of all the code in the .autotest file and replacing it with:
require 'autotest/growl'
require 'autotest/fsevent'
To be clear, that second line should only apply if you're using OSX. I think you can safely get rid of all the "code" in your .autotest file because it relates to manual "red/green" growl notifications, which is no longer necessary once you have the "redgreen" gem installed. In short, you'll end up with a one or two line .autotest file.
Here are some files I installed via gem (some, like rspec-expectations, should be automatically installed as dependencies). The following files are the ones I believe should be relevant to your Autotest setup, the versions are the latest as of 10 minutes prior to writing this response.
autotest (4.4.6, 4.3.2)
autotest-fsevent (0.2.5, 0.2.2)
autotest-growl (0.2.9, 0.2.4)
autotest-rails-pure (4.1.2, 4.1.0)
redgreen (1.2.2)
rspec-core (2.5.1, 2.0.0.beta.18)
rspec-expectations (2.5.0, 2.0.0.beta.18)
rspec-mocks (2.5.0, 2.0.0.beta.18)
rspec-rails (2.5.0, 2.0.0.beta.18)
spork (0.8.4)
webrat (0.7.3)
ZenTest (4.5.0)
Do a gem list command to see which ones you have installed. It might be a simple matter of updating your versions and simplifying your .autotest file (I have mine in my home directory, if you care about location). Also, don't forget that the autotest-fsevent file only applies to OSX.
P.S. You'll probably STILL end up with unexplained autotest errors unless you put some additional code at the very end of your spec/controllers/spec_helper.rb file:
# Webrat configuration
Webrat.configure do |config|
config.mode = :rails
end

Rails colour highlighting for the Test::Unit/rake command?

When running test/unit using the rake test command from the terminal within a rails 3 project directory, the test result output is not coloured. Hence, it cannot be interpreted at a glance.
Is there a way of getting colourised output for the results, as you can get in rspec?
>rspec --colour
I discovered that redgreen was abandoned years ago, and found this solution which works well and requires no script hacking. The output, however, shows which test is being run in real time. So it is a lot longer than built in test output. It does have nice colors.
http://rubygems.org/gems/turn
In my Gemfile:
group :test do
gem 'turn'
end
Then run:
$ bundle install
$ rake test
The gem 'turn' works great. The caveat is that it doesn't seem to work with Mocha, due to monkey-patching issues. If you are using Mocha, you can use the redgreen gem. See instructions above in the approved answer for this question.
Yes, you can use the redgreen gem. Include it in your gemfile:
group :development, :test do
gem 'redgreen'
end
And that's all you need for ruby 1.8. If you're using 1.9, there's a workaround. add the test-unit gem:
group :development, :test do
gem 'redgreen'
gem 'test-unit', '1.2.3
end
It's not perfect with 1.9 - test-unit seems to run an empty test suite after every rake task or generator call, which is harmless but annoying.
I am working on Rails 5.1 / minitest and I was also searching for a solution to make the reporting color. None of these test::unit solutions are working, so I googled and saw this solution. Just add the following:
# Gemfile
gem 'minitest-reporters'
# test/test_helper.rb
require "minitest/reporters"
Minitest::Reporters.use!
Github: minitest-reporters

Resources