---
title: Shoulda Gets Busy with Your Controllers
teaser:
tags: news,web,rails,testing,shoulda
author: Tammer Saleh
published_on: 2007-07-25
---

[A while back][shoulda-intro] we released the [Shoulda Plugin][shoulda-plugin]
to help with testing your ActiveRecord models.  Well, we've been using it in
more and more of our projects, and we've just finished adding some super cool
controller tests.

Check it out!

### Controller Macros

Right off the bat, you get some pretty little test helpers:

```ruby
class UsersControllerTest < Test::Unit::TestCase
  context "on GET to :show for first record" do
    setup do
      get :show, :id => 1
    end

    should_assign_to :user
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash

    should "do something else really cool" do
      assert_equal 1, assigns(:user).id
    end
  end
end
```

That bit of code makes five functional tests, and is super easy to read.  To
take it a  step further - if you've been a good boy and have kept to the latest
restful standards  in your controllers, then you're in luck.

### should_be_restful

You can produce an entire set of tests (over 40) for both html and xml formats
through  the `should_be_restful` helper.

```ruby
class UsersControllerTest < Test::Unit::TestCase
  load_all_fixtures

  def setup
    @controller = UsersController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @user       = User.find(:first)
  end

  should_be_restful do |resource|
    resource.parent = :country
    resource.create.params = { :name => "bob",
      :email => 'bob@bob.com',
      :age => 13
    }
    resource.update.params = { :name => "sue" }
  end
end
```

`Should_be_restful` is very configurable, but as the name states, it's only
intended for testing restful controllers.  We've finished tests for the <abbr
title="HyperText Markup Language">HTML</abbr> and <abbr title="Extensible Markup
Language">XML</abbr>  formats, and <abbr title="1">RSS</abbr>, YAML, and <abbr
title="JavaScript Object Notation">JSON</abbr> will soon be on their way.

Check out the [project page][shoulda-plugin] and the [documentation] for more
information and examples.

[shoulda-intro]: https://thoughtbot.com/blog/introducing-the-shoulda-testing-plugin
[shoulda-plugin]: https://github.com/thoughtbot/shoulda
[documentation]: http://www.rubydoc.info/github/thoughtbot/shoulda-context/master/frames
