Wednesday, November 05, 2008

Test Me On The Rails

I have a set of news sites I visit every day. I never know what I'll find, and usually I do.

On checking "RubyFlow" for November 04, 2008, I saw the following
We've stopped using RSpec... say Rails development team ENTP. They're now back to Test::Unit but improved with Jeremy McAnally's context. Others in the comments suggest shoulda. Is it curtains for RSpec?
Go there.

I sort of idly followed up on it, and found that there was a pretty good discussion of Rails testing options there.

I haven't followed up on all of them yet, except to get a list, which is what I'm posting here.

It's pretty sloppy but it might help someone else. I'm about to update a couple of my sites, maybe add a new one, and get back into the swing of testing with Ruby on Rails, so this is a good time for it all.

First, the most general things.


BDD
There is decent article on Behavior Driven Development at Wikipedia. Go there.
Behavior Driven Development (or BDD) is an Agile software development technique that encourages collaboration between developers, QA and non-technical or business participants in a software project. It was originally conceived in 2003 by Dan North as a response to Test Driven Development, and has evolved over the last few years.

The focus of BDD is the language and interactions used in the process of software development. Behavior-driven developers use their native language in combination with the ubiquitous language of Domain Driven Design to describe the purpose and benefit of their code. This allows the developers to focus on why the code should be created, rather than the technical details, and minimizes translation between the technical language in which the code is written and the domain language spoken by the business, users, stakeholders, project management etc.


Ruby on Rails.org

Ruby on Rails Howtos on Testing has a good list of topics (each item in this list has a URL attached to it):
  • A Guide to Testing the Rails (in depth)
  • A walkthrough of UnitTesting for beginners
  • How to Quickly Test Validation in Unit Tests
  • HowtoFunctionalTestACreateAction
  • HowtoRunOneTest
  • HowtoRakeOneTest
  • HowToUseFixturesWithHABTM
  • HowtoPutSerializedColumnsInYamlFixtures
  • DisableForeignKeyChecksUnderMySql
  • HowtoRunTestsWithCoverage
  • HowToDoTestDrivenDevelopmentInRails
  • HowToWriteYourOwnCustomAssertions
  • HowToUsePreloadedFixtures – save lots of time!
  • HowToTestRailsWithWatir – windows only
  • HowToTestActionMailersAndControllers
  • HowToUseZenTestWithRails?
  • HowToUseRSpecWithRails
  • HowToTestFlash.Now
  • HowToUseFixturesMoreEfficiently
  • HowToLoadFixturesForOneTableIntoDB
  • AgileIntroductionForTestingRails
Non-howto material:
  • Some thoughts on rails testing best practice
  • ZenTest — Automated test scaffolding for Ruby (No info at this site: just "coming soon")
  • Sample CustomAssertions
  • HowToProfileRails
Go there.

On Rails Forum, see "Test/Behavior Driven Development". Go there.

RSpec
RSpec is a Behaviour Driven Development framework for Ruby. It provides two frameworks for writing and executing examples of how your Ruby application should behave: a Scenario Framework for describing behaviour at the application level A Code Example Framework for describing behaviour at the object level.
Go there.
RSpec provides a Domain Specific Language with which you can express executable examples of the expected behaviour of a system." RSpec describes behavior of a system, before it is built, through examples of how it should work after it is built. The examples constitute a test suite.
RSpec tools include:
  • A domain specific language for expressing behavior.
  • A runner to verifying behavior, operating from the command line.
  • Integrated mock objects.
  • A report generator.
  • An intergrated coverage tool, RCov.
  • A coverage verifier to keep coverage up to a minimum level.
  • Integrated diffing.
See also David Chelimsky' blog "on software in process and practice" has "an introduction to RSpec - Part I" (I'm not sure if there is a part II -- at least I couldn't find it). Go there.


Cucumber
Cucumber is a tool that can execute feature documentation written in plain text. Cucumber targets non technical business analysts, interaction designers, domain experts, testers (for the plain text part) and programmers (for the steps, which are written in Ruby).

Cucumber itself is also written in Ruby, but it can be used to "test" code written in Ruby, Java (or web applications written in any language). When IronRuby matures it can be used to "test" .NET code too.

Cucumber only requires minimal use of Ruby programming, so don't be afraid to try it out even if the code you're developing is in a different language. Most programmers should pick up the required Ruby skills and be productive with Cucumber in a few of days.

While Cucumber can be thought of as a "testing" tool, the intent of the tool is to support BDD. This means that the "tests" (plain text feature descriptions with scenarios) are typically written before anything else, and the production code is then written outside-in, to make them pass.
Go there.

Mocha
The Mocha plugin allows mocking and stubbing within tests using a syntax like that of JMock and SchMock.

The big advantage it has over other mocking and stubbing libraries is its ability to mock or stub individual class or instance methods on concrete classes.

Provenance. Mocha has been created by amalgamating a number of techniques developed by James and my Reevoo colleagues Ben, Chris and Paul into a common syntax. It is currently in use on real-world Rails projects.
Go there.

More at "Agile Web Development". Go there.
For the API, see this.

Shoulda
The Shoulda Rails plugin is an eclectic set of additions to the Test::Unit framework that makes writing tests a breeze.

Shoulda: Making Tests Easy on the Fingers and Eyes

The Shoulda gem makes it easy to write elegant, understandable, and maintainable Ruby tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It's fully compatible with your existing tests, and requires no retooling to use.
  • Helpers – context and should give you rSpec like test blocks. In addition, you get nested contexts and a much more readable syntax.
  • Macros – Generate many ActionController and ActiveRecord tests with helpful error messages. They get you started quickly, and can help you ensure that your application is conforming to best practices.
  • Assertions – Many common Rails testing idioms have been distilled into a set of useful assertions.
See more at Thoughtbot, Inc. Go to their projects page.

Go to the Thoughtbot Shoulda page.

See a post at the Thoughtbot blog titled "Introducing the Shoulda Testing Plugin". Go there.

Kyle Banker has a "Shoulda Testing Cheat Sheet" Go there.

Get the cheat sheet PDF.


Test::Unit


Can't leave this one out. It's where everyone starts, and most stay.

There is "A Guide to Testing the Rails" and Ruby on Rails. Go there.
Intended Audience: This article is for fellow Rubyists looking for more information on test writing and how that fits into Ruby On Rails. If you're new to test writing or experienced with test writing, but not in Rails, hopefully you'll gain some practical tips and insight on successful testing.

Assumptions: Just so we're all on the same page here, I'm making a few assumptions about you.
  • You've got Ruby installed and know how to run a Ruby script.
  • You've got Rails installed
  • You've created a basic Rails application with 1 controller and 1 model.
If you haven't accomplished all of the above, you might be jumping ahead of yourself. Check out www.rubyonrails.org for some great beginner's tutorials. Go there.

Factory Girl

More from Thoughtbot: "factory_girl provides a framework and domain specific language for defining and using factories – less error-prone, more explicit, and all-around easier to work with than fixtures".

Go to their projects page.
Go to their factory_girl page.
Don't miss the factory_girl RDoc. Go there.
And there is more at GitHub. Go there.


RR

I couldn't find much about this, but it's out there somewhere.
"RR (Double Ruby) is a test double framework that features a rich selection of double techniques and a terse
syntax."

On GitHub. Go there.
And at Ruby Forge. Go there.


object_daddy
Drop Object Daddy into the vendor/plugins directory of your Rails app. Now every model has a .generate() method which will create a new instance object of that model. If the model in question has no constraints which would make Model.new.save fail then you're set. Those models which do have stronger constraints will want "generators" for those attributes which need special care.
See info on blogtastic. Go there.
And at Ogtastic. Go there.
GitHub has a bit. Go there.


Matchy

It's out there somewhere but it seems to be described only on one blog, which seems to be down right now. No doubt there's more, but I didn't have time to search for hours just for a description.


Context

"Context is a Rails plugin that provides an easy way to determine the context of an incoming remote request so that different responses can be sent. Useful for supporting multiple workflows on RESTful actions."

At RubyForge. Go there.

GitHub has some more info:
DESCRIPTION: If you’ve ever wanted contexts in your Test::Unit tests, then context is for you. Your tests will be easier to read and write without all the magic and extra code smell!

FEATURES/PROBLEMS:
  • Add contexts to Test::Unit tests
  • Small DSL for specifying tests that are pretty
  • Ability to chain context lifecycle methods (coming soon)
Go there.


References:
RubyFlow

1 comments :

  1. Your link to Mocha appears to be broken. Also you can find the Mocha docs here. Thanks, James.

    ReplyDelete