Autotest ruby on rails with rspec - ruby-on-rails

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

Related

Rails, Capybara.using_session(...), visiting a page, and having ruby-debug causes rspec suite seem to hang after run even though all specs passing

Rails 7.0.4
Rspec 3.11.0
Capybara 3.37.1
selenium-webdriver (gem) 4.5.0
with Ruby-debug (gem 'debug', platforms: %i[ mri mingw x64_mingw ]) in Gemfile
whenever I try to add Capybara.using_session call, my suite hangs.
it hangs after it completes all the specs, even when passing.
my driver is set to :
Capybara.default_driver = :selenium_chrome_headless
but I noticed it happens if I change the driver to Capybara.default_driver = :selenium_chrome also
here's the spec that reproduces it:
require 'rails_helper'
describe "client app", type: :feature do
describe "when starting the experience", type: :feature do
# TODO: figure out why capy is hanging here
it "can load the home page" do
# THIS CAUSES AFTER-RSPEC HANG even though all specs pass
Capybara.using_session("client session") do
visit "/"
end
end
it "loads with a client id" do
visit '/'
end
end
end
I have isolated it to these THREE conditions :
using Capybara.using_session and visiting a page using visit inside the using_session block (if I remove either the visit or the using_session, it works)
In another test in the same suite, visiting any other page using visit (if I remove the visit call in the other spec, it works)
the debug gem (ruby-debug) (if I remove gem 'debug', platforms: %i[ mri mingw x64_mingw ] from the Gemfile)
This is a very strange bug.
Capy hang looks like:
my spec/rails_helper.rb file is
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Capybara.default_driver = :selenium_chrome_headless
IMPORTANT: Although I have isolated it this far, I am strangely unable to isolate it any further or even able to reproduce it in a new Rails app, even though this Rails app is just a month old.
Here, I have taken the app where it is broken and removed everything else (really, I removed EVERYTHING and the hang still happens).
I have compared the remaining code carefully to a newly generated app and bizarrely I cannot reproduce the same bug (Using the exact same spec and setup) in a new app, so it must involve a 4th unknown variable I cannot see yet.
REPRODUCTION
Bare-bones reproduction app here:
https://github.com/jasonfb/StrangeCapybaraHang
This app was created as a FORK of my app, then I removed everything non-essential. Notice that it contains almost nothing, and only 1 spec.
When I run rspec on this app (broken), see "DEBUGGER: Attaching after process 77726 fork to child process 77733" AND the suite hangs after it runs
Here, on this 2nd attempt to recreate the problem, I tried to go forward to create the bug: https://github.com/jasonfb/StrangeCapyHangForward
I setup a new Rails app and then attempted to recreate all of the conditions explained above to produce the bug... BUT... I can't reproduce the bug in this new app!!
So that means even though I've identified the 3 elements of the bug, there MUST be a 4th element I am not seeing yet.
Notice that in this app, it has same exact spec (see spec/system/test_capy_hand_spec.rb), the same exact rails_helper file, the same exact Gems, etc as the other repo where the bug manifests.
However, on this app, I never see "DEBUGGER: Attaching after process 77726 fork to child process...." even when debug Gem is in the Gemfile. Why is that?
I also DO NOT see the hang.
2022-12-15 SOLVED!
TL;DR
The issue is in the selenium-webdriver gem. Upgrade from 4.5.0 in your StrangeCapybaraHang project to 4.7.1 and it should solve your problem immediately.
Reproducing from Scratch
Fork the StrangeCapyHangForward repo.
Run ./bin/setup && yarn build. Note, if esbuild is not globally installed, you'll need to add it with yarn add esbuild since it's missing from package.json.
Downgrade the selenium-webdriver gem to the same version that is in StrangeCapybaraHang. For example, in your Gemfile's :test group: gem 'selenium-webdriver', '4.5.0'
bundle install && rspec. It hangs.
Example of Gemfile with not working selenium-webdriver gem added
# Gemfile of StrangeCapyHangForward
group :development, :test do
gem 'debug', platforms: %i[ mri mingw x64_mingw ]
gem 'rspec-rails'
# add the older version of the gem
gem 'selenium-webdriver', '4.5.0'
end
Steps to Fix
Add gem 'selenium-webdriver', '4.7.1' to your Gemfile in StrangeCapyHangForward.
bundle install
Run rspec. No more hang!
Note: You may have to grep and kill existing rspec processes. For example:
ps -ax | grep rspec
# take process ids for all the above and run (substituting 71529 for your pids):
kill -9 71529
Example Gemfile with working selenium-webdriver gem added
# Example Gemfile with working version of selenium-webdriver
group :development, :test do
gem 'debug', platforms: %i[ mri mingw x64_mingw ]
gem 'rspec-rails'
# add selenium-webdrive gem coded to 4.7.1
gem 'selenium-webdriver', '4.7.1'
end
I believe the issue is related to concurrency and rspec not properly killing all the test processes, although this is difficult to confirm. We had big trouble with our test suite and concurrency, too, until we forcibly upgraded many of our gems (including dependencies).
For example, before upgrading run rspec a few times and grep your processes: ps -ax | grep rspec. You'll see lots of existing ones there. I did check the selenium-webdrive changelog to see if there was anything obvious that would cause this, but I didn't see anything.
Anyways, hope this helps! Good luck!
Try removing the debug gem, as it is probably causing the issue with capybara.

How do I get awesome_print to work without using ~/.irbrc file?

I want to use awesome print without putting it in my rails 5 app. Just in the console. The documentation for requiring it in irb is not working.
That's because bundler isolates the gems available to load to what's in your Gemfile.
The best way to get around this is to add the gem to your Gemfile
gem 'awesome_print', require: false, group: :development
And in your .irbrc, you can require it, so that it is only enabled for you:
begin
require 'awesome_print'
rescue LoadError => err
warn "could not require awesome_print: #{err}"
end
However, if you aren't permitted to add awesome_print to your repository for whatever reason, there are a few hacks to get gems installed, but not in your Gemfile to load in this GitHub Gist.
One such example that could be placed at the top of your .irbrc:
# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*#global/).first
$LOAD_PATH.concat(Dir.glob("#{global_gemset}/gems/*/lib")) if
global_gemset
end
cd your/rails/project
irb
inside irb, run:
require 'awesome_print'
require './config/environment'
and you have both rails console and awesome_print gem while the gem is installed outside of the bundler.

Ruby: mnitest-rails + mocha

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.

How to require the forked gem lib file ? Name conflicts?

I had to fork the gem Thor, coz my cli has one command run which is reserved in the Thor lib itself, changed its name to millisami-thor just in the .gemspec as follows:
Gem::Specification.new do |s|
...
s.name = 'millisami-thor'
...
end
and to use it, I pushed the gem under the name millisami-thor to rubygems.org and in the Gemfile of my cli project, I put gem 'millisami-thor', :require => 'thor'
Now while testing bundle exec cucumber features or to try out the executable, just did ./bin/executable --params and it worked out.
Now, I build the gem with gem build gemname.gemspec that generates gemname.gem and installed with gem install gemname.gem and it gets installed as well as the binary too. Fine, till here.
Now, when I use the binary cmd like executable --params, it looks for the original thor library instead of the forked one.
I figured out that this was due to the require ... in the executable.
require 'thor'
require 'fileutils'
require 'gemname/cli'
Cf::CLI.start
coz in there I explicitly required the original thor.
Now, when I change it to require 'millisami-thor, it cannot find and says:
...
custom_require.rb:36:in `require': no such file to load -- millisami-thor (LoadError)
...
In the Gemfile, I could have done gem 'millisami-thor, :require => 'thor' so that it loads the forked gem.
But how can I do the same if its just the require 'millisami-thor' ?
The only option I can think of is to change all the class names to 'MillisamiThor' instead of 'Thor' and the file names too. But this will be too messy and ugly.
I could have spotted this if I had installed my gem and test it before. But I did just in the test environment, in which the bundler requires the millisami-thor's thor file, so I didn't have this problem till today.
Is there any other way out to achieve this without any messy hacks?
You can keep the original gem setup and point Bundler to your git repository fork:
gem 'thor', :git => 'git://github.com/yourname/thor.git', :require => 'thor'
or even a local path
gem 'thor', :path => '/path/to/thor.git', :require => 'thor'

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