I'm working on a gem which depends on Rails and Rack. I would like to write bunch of integration tests which would be automatically run on different versions of rails and rack gems. Do you know any good read about this or have some experiences in this topic?
It sounds like thoughtbot's appraisal gem is the kind of thing you're looking for.
Set up your gems:
appraise "rails-3" do
gem "rails", "3.2.14"
end
appraise "rails-4" do
gem "rails", "4.0.0"
end
Then run your tests:
appraisal rails-3 rake test
Related
I'm using Turn gem in a rails app to colourise my Test::Unit output, and I find the following output before any of my tests run:
stephen#x5:~/code/ruby$ rake test
gem install minitest
gem install minitest
... tests seem to run just fine, and are nicely coloured
This happens everytime I run them. What is the deal here? Am I supposed to be using minitest instead of Test::Unit?
What is the best way to colourise Test::Unit output? Just looking for red and green.
gem 'redgreen' worked great for me (on Ruby 1.9.2 and Rails 3.0.7) with Test::Unit.
Add the above to your Gemfile.
On RubyGems.
I've come across a few tutorials where the author declares two versions of the same gem in the gemfile, even in the same group.
"haml" and "haml-rails"
"rspec" and "rspec-rails"
"cucumber" and "cucumber-rails"
and there are more examples of this....
Why is this done? Is this a better way to work with these gems rather than declaring a single gem?
Thanks
Well, these are not the same gems. Rails versions usually extend the standard libraries.
But because foo-rails has foo in its dependencies (see example here), you just need foo-rails in your Gemfile (Bundler is just great).
cucumber-rails is not the same gem as Cucumber. It's got Cucumber as a dependency, as well as some Rails-specific stuff. So as apneadiving said, if you include cucumber-rails in your Gemfile, it should load Cucumber too.
TL; DR:
I don't know how to get RSpec::Matchers to work in my test/unit/..._test.rb tests has any one succeeded? should I migrate all those tests to the spec folder?
So, I'm using cucumber, Test::Unit, and before I started the project there were a bunch of spec tests.
When I was reading up on what testing framework to use for my unit tests, I found RSpec, and thought it was pretty cool... but as it turns out, I didn't actually use any Rspec until I found out about the should be_nil / be_whatever methods. I thought those were pretty cool.
I stuck with sticking tests in the test/(unit|functionals) folders, because it made the most sense (I thought having unit and functional tests in a "spec" folder was really weird and unconventional. I also began using shoulda, which enabled me to do should "test name" rather than it "should..." which I thought was more concise, and all around more intuitive.
But I recently started using cucumber for view testing, and... that resulted in tons of conflicting gem issues. It was a terrible hassle to get cucumber working with Factory girl, capybara, and the like.
I eventually had to migrate to bundler, because I could never get my environment working.
Anyway, after about a week of messing with the testing environment, cucumber works, functional tests work, and I feel like unit test WOULD work if I hadn't intially used .should be_valid / be_whateven methods.
Has anyone else gotten RSpec::Matchers to work in this scenario?
relevant part of GemFile:
group :test do
gem "cucumber", "~>0.10.3"
gem "cucumber-rails", "0.3.2"
gem "launchy"
gem "hpricot"
gem "gherkin", "~>2.4.0"
gem "capybara", "0.4.1.2"
gem "rspec", "~> 1.3.2"
gem "rack"
gem "rspec-rails", "~> 1.3.3"
gem "webrat", "0.7.0"
gem "database_cleaner"
gem "factory_girl", "2.1"
gem "shoulda", :require => nil
gem "shoulda-matchers", :git => "https://github.com/thoughtbot/shoulda-matchers"
gem "awesome_print"
gem "cobravsmongoose"
end
Here is the link to :
the rspec expectations page talking about integration with Test::Unit
My experience with testing with cucumber, rspec and test::unit leads me to believe that it is all about comfort as long as you are getting the coverage. I like rspec much more than test. If you are not really comfortable with test unit, definitely try rspec. I also know people (and am starting to lean this way myself) that just use cucumber and skip rspec all together (but this can get into a bit of a "religious" discussion).
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
I'm trying to install RSpec as a gem after having it installed as a plugin. I've gone ahead and followed the directions found here http://github.com/dchelimsky/rspec-rails/wikis for the section titled rspec and rspec-rails gems. When I run ruby script/generate rspec, I get the error Couldn't find 'rspec' generator. Do only the plugins work? If so, why do they even offer the gems for rspec and rspec-rails? I'm running a frozen copy of Rails 2.1.2, and the version of rpsec and rspec-rails I'm using is the newest for today (Nov 7, 2008) 1.1.11.
EDIT Nov 12, 2008
I have both the rspec and rspec-rails gems installed. I've unpacked the gems into the vender/gems folder. Both are version 1.1.11.
Since RSpec has been become the default testing framework in Rails you no longer need to create spec docs via the rspec generators:
Rails 2 RSpec generator
rails generate rspec_model mymodel
Rails 3 RSpec generator
With RSpec as the default testing framework simply use Rails' own generators. This will construct all of the files you need including the RSpec tests. e.g.
$rails generate model mymodel
invoke active_record
create db/migrate/20110531144454_create_mymodels.rb
create app/models/mymodel.rb
invoke rspec
create spec/models/mymodel_spec.rb
Have you installed both rspec and rspec-rails gems?
script/generate rspec
requires rspec-rails gem to be installed.
For Rails 3 and rspec 2+
You must make sure you include 'rspec' and rspec-rails' in your Gemfile
Run Bundle Install
then run rails g rspec:install
If you are using rails 2.3 You need to use
ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git -r 'refs/tags/1.3.3'
and then
ruby script/generate rspec
Is there supposed to be an 'rspec' generator? I've only used the following:
script/generate rspec_model mymodel
script/generate rspec_controller mycontroller
I've had this problem before, it boiled down to the version of RSpec I had not working with the version of Rails I was using. IIRC it was a 2.1 Rails and the updated RSpec hadn't been released as a gem. In fact, 1.1.11 is the gem I have, which would be the latest available (ignoring github gems), so I'm pretty sure that's exactly what my problem was.
I've taken to just using the head of master rspec with whatever version of Rails I happen to be on, it seems stable to me (and isn't going to break things in production, unless somehow a test broke with a false positive).
I do it with git using submodules, for example:
git submodule add git://github.com/dchelimsky/rspec.git vendor/plugins/rspec
git submodule add git://github.com/dchelimsky/rspec-rails.git vendor/plugins/rspec_on_rails
In case anyone is wondering about Rails 3 now,
this seems to do the trick for me:
http://github.com/rspec/rspec-rails/blob/29817932b99fc45adaa93c3f75d503c69aafcaef/README.markdown
I'm using rails 2.3.9. I started of trying to use the gem(s) but just couldn't get the generator for rspec to show up. Then I installed the plugin(s) using the instructions on https://github.com/dchelimsky/rspec/wiki/rails and that did the trick.
On Fedora 9 (OLPC) I did:
$ sudo gem install rspec
$ sudo gem install rspec-rails
Those got me to where I could run
$ ruby script/generate rspec
This worked for me, whereas the git instructions did not work.
If you are using bundler version 1.0.8 you should $ gem update bundler to a newer version 1.0.9.
I had the same symptons and updating bundler helped me out.
Now $ rails g is using gems defined in the Gemfile. Also I grouped my gems like this:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', :require => 'sqlite3'
group :test, :development do
gem 'capybara', '0.4.1.1'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'rspec-rails', '~> 2.4'
gem 'launchy'
end
(Note that test gems are also in the :development group.)
Have a nice day :)
Lukas
If you type script/rails generate, the only RSpec generator you'll actually see is rspec:install. That's because RSpec is registered with Rails as the test framework, so whenever you generate application components like models, controllers, etc, RSpec specs are generated instead of Test::Unit tests.
Please note that the generators are there to help you get started, but they are no substitute for writing your own examples, and they are only guaranteed to work out of the box for the default scenario (ActiveRecord & Webrat).
https://github.com/rspec/rspec-rails
You might need to run bundle exec :
bundle exec rails g rspec:install
You'll need to do
sudo gem install cucumber-rails