rails swtiching to rspec from default test unit - ruby-on-rails

I have a rails 4 app. I have no tests at the moment, but when I created the app I didn't skip the default test unit so there are some empty test files (and other default settings) in my app.
Now I would like to use rspec + capybara, but don't know what the necessary extra steps are to properly install those and make sure the testing will work fine. I saw some answers on stackoverflow but thoose were pretty old.
As far as I know the installation looks like this if test unit is skipped on creation:
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'capybara'
end
then
rails g rspec:install
Can sby tell me what the extra steps are?

Follow these steps:
add code to your_app/config/application.rb file:
config.generators do |g|
g.test_framework :rspec
end
add below code to your_app's Gemfile:
group :test, :development do
gem 'rspec-rails'
end
save it, and run bundle install to install rspec gem
Initialize the spec/ directory
rails generate rspec:install
Use the rspec command to run your specs:
bundle exec rspec
Hopefully it helps.

Related

Minitest-Rails Spec-Style Tests by Default

Question: Using Rails 5 & Minitest-Rails is there a way to default new Rails apps to default to Spec-style testing?
I teach TDD and it's annoying to have to have the students convert each time we make a new app.
You could create a template.rb file with following configuration:
gem_group :development, :test do
gem "rspec-rails"
end
after_bundle do
`rails g rspec:install`
end
And then build a new Rails project using the following command
rails new my_app -T -m /path/to/template.rb
It will build a new Rails application, add Rails RSpec gem to its Gemfile and execute the install step for RSpec.
Otherwise you could provide a pre-built Rails Git repository and build on top of that.
References:
Rails Application Templates — Ruby on Rails Guides
rspec/rspec-rails: RSpec for Rails-3+
Looks like you've already done the hard work of answering your question. Though if you're teaching a class with an opinionated group of test gems, and a modified test_helper.rb and a modified application.rb, you may wish to consider writing your own gem that your students can add to their Gemfile. The gem could have the test gems you want as dependencies, and then they can install everything else they need with something like:
bin/rails generate <gem_name>:install
Here's a gem I wrote that you can fork or modify or just use as inspiration.
https://github.com/schadenfred/testable
I actually stole your config code for the above gem, which you can see expressed in inside a generator that lives here:
lib/generators/installer/install_generator.rb
It looks like in config/application.rb you just have to add:
config.generators do |g|
g.test_framework :minitest, spec: true
end
However there's not an automatic way to make Minitest-Rails default to spec style testing.
I could go to rspec, but would rather stay with Minitest for the moment as we teach our students Minitest from the beginning.
Ok so #sonna had 90% of what I was looking for.
I ended up with help creating a .railsrc file with
-d postgresql
-m ~/.template.rb
And a template with:
# .template.rb
# Gems we've talked about in class, but which have absolutely nothing to do
# with setting up spec-style testing.
# Included here for convenience.
gem_group :development do
# Improve the error message you get in the browser
gem 'better_errors'
# Use pry for rails console
gem 'pry-rails'
end
# Add some extra minitest support
gem_group :test do
gem 'minitest-rails'
gem 'minitest-reporters'
end
# Add some code to some files to support spec-style testing
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
<<-'RUBY'
# Force new test files to be generated in the minitest-spec style
config.generators do |g|
g.test_framework :minitest, spec: true
end
RUBY
end
# Things to do after all the gems have been installed
after_bundle do
# Run rails generate minitest:install
generate "minitest:install", "--force"
# Add minitest reporters support. This must be run after
# rails generate minitest:install, because that command
# changes test/test_helper.rb
inject_into_file 'test/test_helper.rb', after: 'require "minitest/rails"' do
<<-'RUBY'
require "minitest/reporters" # for Colorized output
# For colorful output!
Minitest::Reporters.use!(
Minitest::Reporters::SpecReporter.new,
ENV,
Minitest.backtrace_filter
)
RUBY
end
end
This sets up my project with postgres for DB, and Minitest-rails using spec-style tests and includes minitest-reporters.

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

Rails 3.2 can't find my tests, thinks they are in "test" directory

I am trying to user RSpec and failing to configure MiniTest/Spork/Capybara/Guard. My Gemfile is all set and I ran the command to install RSpec.
Unfortunately, the application continues to "think" my tests are in the test directory. Sorry, they're not. They're in specs, and the framework should really know that.
Maybe it's some sort of configuration issue from trying to get the impossible stack above to work that I still have in place. Where does Rails "decide" in which directory to look for tests? RSpec itself is the one that created the spec directory.
This is how rspec-rails should be included in your Gemfile.
group :development, :test do
gem 'rspec-rails', '2.11.0'
end
Make sure it is added in both :development and :test group.

Why don't spec/controller/pages_controller_spec.rb tests get triggered when I modify html.erb pages?

Summary of question: Is autotest supposed to 'trigger' any of my /pages_controller_spec.rb tests when I change any of the pages referenced in the file?
Example: I have a test in the spec/controller/pages_controller_spec.rb file that tests for the title of a specific page - e.g. about.html.erb - but when I edit that file (the about file), none of my autotests trigger. All tests pass if I reload autotest or run a manual 'rspec spec/' - they just aren't triggering automatically, which is what I want.
This appears to be by design, because when I run autotest -v, it shows that there are no tests associated with the individual pages in question - excerpt:
...
No tests matched app/views/pages/contact.html.erb
No tests matched app/views/pages/help.html.erb
No tests matched app/views/pages/home.html.erb
...
All of my tests run green btw, and autotest + spork/etc triggers just fine when I edit files where I have a specific _spec file associated with it (e.g. the above file in my example & app/controllers/pages_controller.rb). It's just these html.erb files that are bothering me really.
So I guess my real question boils down to this: is this user error or does autotest really ignore these related html.erb files? Do I have to write explicit tests to hit these files if I want them tested? This seems wrong to me because I could completely break my tests by removing lines/etc from the html.erb file (but NOT changing the base pages_controller.rb file), and in that case autotest wouldn't trigger a test..
Environment details, etc:
I'm running rails 3.0.1 with ZenTest/autotest and Spork. This problem exists both with and without Spork running, so I'm guessing it's not relevant, but here is my Gemfile nevertheless:
source 'http://rubygems.org'
gem 'rails', '3.0.1'
gem 'sqlite3-ruby', :require => 'sqlite3'
group :development do
gem 'rspec-rails'
end
group :test do
gem 'rspec-rails'
gem 'webrat', '0.7.1'
gem 'test_notifier'
gem 'spork', '~> 0.9.0.rc'
end
This is Ubuntu 10.1 using a simple Kate/Terminal "IDE" if it matters. But again I think this is a configuration question and not an environment issue or bug.
It is my understanding that autotest doesn't not run when you make changes to views. You can add a configuration option though to add the specific path to be monitored. Here is an example from the RSpec github wiki for adding support for integration tests:
Autotest.add_hook :initialize do |at|
at.add_mapping(%r%^spec/(integration)/.*rb$%) {|filename, _|
filename
}
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