Skip to content

Instantly share code, notes, and snippets.

@ZenCocoon
Created February 20, 2011 19:05
Show Gist options
  • Save ZenCocoon/836219 to your computer and use it in GitHub Desktop.
Save ZenCocoon/836219 to your computer and use it in GitHub Desktop.
RSpec2, Rails3 and testing your generic controller like admin_controller.rb
#
# # What's the problem
#
# I wanna test a before filter that render errors in XML and JSON if failing
# When testing it from a classic controller it works fine, when testing from
# the ApplicationController directly it fails
#
#
# # Some code please...
#
# specs/controller/admin_controller_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe AdminController do
controller(AdminController) do
def index
render :text => 'ok'
end
end
it "should include errors properly formated" do
get :index, :format => 'xml'
response.body.should == "MyProperlyFormatedXML"
end
end
# app/controller/admin_controller.rb
class AdminController < ApplicationController
before_filter :block_something
protected
def block_something
respond_to do |wants|
wants.xml {
render(:xml => {:error => "details"}.to_xml(:root => "errors"),
:status => :unprocessable_entity)
}
end
end
end
# Error from tests
Failure/Error: get :index, :format => format
TypeError:
can't convert nil into String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment