1. Aug 29th, 2007

    be_accepted, be_created, be_forbidden, be_found

    Little addition to your Rails test suite:

    module ActionController
      class AbstractResponse
        StatusCodes::SYMBOL_TO_STATUS_CODE.each do |symbol, code|
          define_method("#{symbol}?") { self.code == code.to_s } unless instance_methods.include?("#{symbol}?")
        end
      end
    end

    You can now assert your favorite status code:

    post :create, params
    assert response.created?

    And with RSpec, you too can be that glorious status code:

    post :create, params
    response.should be_created
    1. Sep 14th, 2011

      dave

      Following the internal re-organization of Rails 3, use this code instead:

      module ActionDispatch
      class Response
      Rack::Utils::SYMBOL_TO_STATUS_CODE.each do |symbol, code|
      define_method(“#{symbol}?”) { self.code == code.to_s } unless instance_methods.include?(“#{symbol}?”)
      end
      end
      end

    Your comment, here ⇓