Class ThoughtBot::Shoulda::Controller::ClassMethods::ResourceOptions
In: lib/shoulda/controller_tests/controller_tests.rb
Parent: Object

A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.

Example:

  class UsersControllerTest < Test::Unit::TestCase
    load_all_fixtures

    def setup
      ...normal setup code...
      @user = User.find(:first)
    end

    should_be_restful do |resource|
      resource.identifier = :id
      resource.klass      = User
      resource.object     = :user
      resource.parent     = []
      resource.actions    = [:index, :show, :new, :edit, :update, :create, :destroy]
      resource.formats    = [:html, :xml]

      resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
      resource.update.params = { :name => "sue" }

      resource.create.redirect  = "user_url(@user)"
      resource.update.redirect  = "user_url(@user)"
      resource.destroy.redirect = "users_url"

      resource.create.flash  = /created/i
      resource.update.flash  = /updated/i
      resource.destroy.flash = /removed/i
    end
  end

Whenever possible, the resource attributes will be set to sensible defaults.

Classes and Modules

Class ThoughtBot::Shoulda::Controller::ClassMethods::ResourceOptions::ActionOptions
Class ThoughtBot::Shoulda::Controller::ClassMethods::ResourceOptions::DeniedOptions

External Aliases

parent -> parents
parent= -> parents=

Attributes

actions  [RW]  Actions that should be tested. Must be a subset of VALID_ACTIONS (default). Tests for each actionw will only be generated if the action is listed here. The special value of :all will test all of the REST actions.

Example (for a read-only controller):

  resource.actions = [:show, :index]
create  [RW]  ActionOptions object specifying options for the create action.
denied  [RW]  DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
destroy  [RW]  ActionOptions object specifying options for the desrtoy action.
formats  [RW]  Formats that should be tested. Must be a subset of VALID_FORMATS (default). Each action will be tested against the formats listed here. The special value of :all will test all of the supported formats.

Example:

  resource.actions = [:html, :xml]
identifier  [RW]  Name of key in params that references the primary key. Will almost always be :id (default), unless you are using a plugin or have patched rails.
klass  [RW]  Name of the ActiveRecord class this resource is responsible for. Automatically determined from test class if not explicitly set. UserTest => "User"
object  [RW]  Name of the instantiated ActiveRecord object that should be used by some of the tests. Defaults to the underscored name of the AR class. CompanyManager => :company_manager
parent  [RW]  Name of the parent AR objects. Can be set as parent= or parents=, and can take either the name of the parent resource (if there‘s only one), or an array of names (if there‘s more than one).

Example:

  # in the routes...
  map.resources :companies do
    map.resources :people do
      map.resources :limbs
    end
  end

  # in the tests...
  class PeopleControllerTest < Test::Unit::TestCase
    should_be_restful do |resource|
      resource.parent = :companies
    end
  end

  class LimbsControllerTest < Test::Unit::TestCase
    should_be_restful do |resource|
      resource.parents = [:companies, :people]
    end
  end
update  [RW]  ActionOptions object specifying options for the update action.

[Validate]