Created
February 9, 2012 06:29
-
-
Save samwgoldman/1777846 to your computer and use it in GitHub Desktop.
Helpful macros for rspec controller specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RackTestHelpers | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def self.define_action(action) | |
define_method action do |*args, &block| | |
options = args.extract_options! | |
options[:http_method] = action | |
options[:controller_method] = args[0] | |
args[0] = [action.to_s.upcase, "#" + options[:controller_method].to_s].join(" ") | |
args << options | |
context(*args, &block) | |
end | |
end | |
define_action :get | |
define_action :post | |
define_action :put | |
define_action :delete | |
define_action :head | |
define_action :options | |
def it_should_flash(type, message) | |
it "should set the flash to \"#{message}\"" do | |
do_request | |
flash[type].should eq(message) | |
end | |
end | |
def it_should_redirect_to(&block) | |
it "should redirect to #{description}" do | |
url = instance_eval(&block) | |
do_request | |
response.should redirect_to(url) | |
end | |
end | |
def it_should_render_template(template) | |
it "should render the #{template} template" do | |
do_request | |
response.should render_template(template) | |
end | |
end | |
end | |
def do_request | |
params = if respond_to?(:params) then send(:params) else nil end # of story | |
send(example.metadata[:http_method], example.metadata[:controller_method], params) | |
end | |
end | |
RSpec.configure do |config| | |
config.include RackTestHelpers, :type => :controller | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment