Automated code sanity check tools for Ruby - ruby-on-rails

What tools do you use for automated code sanity checks and adhering to the coding conventions in your Ruby apps? How do you incorporate them into your process? (I mean tools like roodi, reek, heckle, rcov, dcov, etc.)

I'd suggest taking a look at RuboCop. It is a Ruby code style checker based on the Ruby Style Guide. It's maintained pretty actively and it's based on standard Ruby tooling (like the ripper library). It works well with Ruby 1.9 and 2.0 and has great Emacs integration.

The metric_fu gem might be perfect for what you need. From it's README: "Metric-fu is a set of rake tasks that make it easy to generate metrics reports. It uses Saikuro, Flog, Rcov, and Rails'
built-in stats task to create a series of reports. It's designed to integrate easily with CruiseControl.rb by placing files in the Custom Build Artifacts folder." Since they converted it to a gem, it works with non-Rails applications as well. I'll bet you could add hooks for other tools as well.

There was some good discussion on this topic on the On-Ruby blog recently. For my personal development process I build quality tools into my tests, but only after all other tests have run. So I have a top-level rake task that looks something like this:
desc 'Runs all unit tests, acceptance tests and quality checks'
task 'test' => ['test:spec', 'test:features', 'test:quality']
I allow myself to commit if the last suite "fails", but I do try to get them to zero at least once each day.

Related

What is a Good Automated Testing Platform for RSpec?

For testing purposes for Ruby/Rails I use RSpec and I have well over 1000 tests that are already written in my application. Once the app goes live I want to have the tests run once a day on the production server (since all the code is there) and see if anything fails. The test itself will be run against test data and now production data. This is an effort so that any new code that is added in the future will not cause anything to unknowingly break.
I have found a few solutions:
Travis-CI (only open-source ... not suitable for closed-source projects)
Jenkins-CI (not sure if it works with or well with Ruby/RSpec/Rails)
Watir (not sure if Ruby/Rspec works with it, but the tool itself is written in Ruby).
Preferably something that checks the codebase daily and then emails me when something isn't working.
I also plan on integrating JavaScript testing with the a testing library of my choice (I just need the automation platform for testing it).
Can someone provide me some insight as to which tool to use? Or does anyone have any other tools to recommend?
Jenkins-CI works great with rspec, and can run your jasmine javascript testing, and your cucumber javascript tests as well.
The only thing I'd recommend is not to test on your production server itself. When you push changes to your source-control repository, Jenkins will download the new code and run your tests there. When you're green (tests pass), push the code to production.

Rails 3 RSpec 2 NetBeans integration

NetBeans 6.9 provides a custom Runner class for RSpec to be integrated into the IDE. I'm trying to get my Rails 3 applications specs to be correctly displayed inside NetBeans, but RSpec 2 seems no longer to support custom Runner classes in general.
Any ideas how to get the specs into the IDE anyway?
Just in. Oracle has just announced they are withdrawing support for Rails in future version of NetBeans. Time to start looking at other IDE options.
http://news.ycombinator.com/item?id=2148161
So far (NB 6.9.1) the only way I know to run Rspec2 tests from inside NetBeans is by using rake tasks. But I was not able to make it work with UI Test Runner, because of this and few other problems. So the best way is to avoid invoking UI runner, this can be done in many ways:
Disable it via tools -> options -> miscellaneous -> Ruby
modify project.properties file
give other name to task than 'spec', so naming task as 'rspec' will avoid invoking UI runner
This way you will have just test results in output pan, but it is still usable, because you can click anywhere on stack trace, and NB will take you immediately to that file:line.
There is one thing left, auto generated by NB Rakefile has not valid task (for Rails projects, there is NO such problem), to make it work one needs at least:
require 'rspec/core/rake_task'
Rspec::Core::RakeTask.new(:rspec)
I know this is not what you are expecting but you might want to check RubyMine3 out it comes out of the box, you do need to buy a licence but at least you can check it out in the 30 day trial
I am using RVM.
And at the minimum I wanted to be able to run my Ruby 1.9.2 / Rails 3 / RSpec 2 specs from inside the IDE and be able to click on stack traces for Netbeans to open the right files and lines.
I found a work-around for that:
Put somewhere in the project a ruby file that shells out to run the spec suite.
E. g. my ruby file has the following content:
system <<EOF
time ~/.rvm/wrappers/ruby-1.9.2-p290#default/rspec --drb spec
EOF
Change the ruby version and gemset as you need it.
The major limitation:
I cannot just run only a single spec. For that I have to change the "spec" parameter to the target spec file (which isn't such a big deal though).
Netbeans is fairly sluggish running specs (using rspec1 here), would recommend running specs from command line.

What Test Environment Setup do Committers Use in the Ruby Community?

Today I am going to get as far as I can setting up my testing environment and workflow. I'm looking for practical advice on how to setup the test environment from you guys who are very passionate and versed in Ruby Testing.
By the end of the day (6am PST?) I would like to be able to:
Type one 1-command to run test suites for ANY project I find on Github.
Run autotest for ANY Github project so I can fork and make TESTABLE contributions.
Build gems from the ground up with Autotest and Shoulda.
For one reason or another, I hardly ever run tests for projects I clone from Github. The major reason is because unless they're using RSpec and have a Rake task to run the tests, I don't see the common pattern behind it all.
I have built 3 or 4 gems writing tests with RSpec, and while I find the DSL fun, it's less than ideal because it just adds another layer/language of methods I have to learn and remember. So I'm going with Shoulda. But this isn't a question about which testing framework to choose.
So the questions are:
What is your, the SO reader and Github project committer, test environment setup using autotest so that whenever you git clone a gem, you can run the tests and autotest-develop them if desired?
What are the guys who are writing the Paperclip Tests and Authlogic Tests doing? What is their setup?
Thanks for the insight. There are tons of resources describing how to use the different testing frameworks, but almost nothing on the actual setup and workflow. Looking for answers that will make me a more effective tester.
The most common convention probably is rake test, rake spec, or maybe even just rake.
Of course, there is no question that this will fail with many projects, in particular the ones without tests or specs.
It might be possible to parse the output of rake -T if a Rakefile is there, and act on that, but there really is no way you will cover ALL projects on GitHub.

What is the current standard way to deploy a Rails app?

Up until now I've been deploying Rails apps to our Apache/Passenger setup using a simple Rake task that I wrote. I haven't tried to mess around with Capistrano or Vlad the Deployer.
However, now more developers are coming on board, and I'm interesting in arranging things so that the deployment process runs the tests first and won't deploy unless they all pass. So I'm revisiting the question.
It's been a while since I looked into this. What are most people doing these days? Still using Capistrano? Writing individual Rake tasks? Something else?
Capistrano is still the standard for typical Rails deployments, yes.
We're using Capistrano and Integrity for a CI server. Integrity is quite easy to hack on and you could really easily set it up to automatically deploy on a pass of all tests, and I'd recommend all of them as good tools; Integrity has plenty of plugins available. We currently have Integrity spit out each build's pass/fail and code coverage % into an IRC channel and manually deploy.

Integrate test process with Rspec and Cucumber in a plugin using Desert

I'm developing some rails plugins with desert, and I would like to use tests with RSpec and Cucumber in the development. RSpec is integrated by default in Desert plugins, but it gives me some bugs.
Finally I have created a minimal application which use my plugin, and I try to test it like a normal application without plugin. But I dislike that solution, the plugin lost its modularity, and RSpec still returns me some bugs (he can't find the models/views/controllers located in the plugin...).
So am I the only one who try to test a Desert plugin (or even in a classic plugin) ? May anyone know those matters and get a solution ?
I was working with 'Desert' plug-ins in a legacy Rails 2.2.2 app and the following helped me http://pivotallabs.com/users/joe/blog/articles/985-testing-desert-plugins-in-isolation
You should not use desert plugin, it's an 'old thing'.

Resources