Created
January 12, 2009 23:22
-
-
Save rmm5t/46233 to your computer and use it in GitHub Desktop.
Less verbose controller macros for shoulda.
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
# Helper controller macros that define: | |
# on_get, on_post, on_put, on_delete | |
module ControllerMacros | |
[:get, :post, :put, :delete].each do |method| | |
class_eval <<-RUBY | |
def on_#{method}(action, options = {}, &block) | |
on_http_method(#{method.inspect}, action, options, &block) | |
end | |
RUBY | |
end | |
def on_http_method(method, action, options_hash_or_lambda = {}, &block) | |
options = options_hash_or_lambda | |
description = "on #{method.to_s.upcase} to #{action.inspect}" | |
if options.is_a?(Hash) | |
options.each do |key, value| | |
description << " with #{key}=#{value.inspect}" | |
end | |
end | |
context description do | |
setup do | |
opts = options.is_a?(Proc) ? options.bind(self).call : options | |
send(method, action, opts) | |
end | |
merge_block(&block) | |
end | |
end | |
end | |
class Test::Unit::TestCase | |
extend ::ControllerMacros | |
end |
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
# ____ _____ _____ ___ ____ _____ | |
# | __ )| ____| ___/ _ \| _ \| ____| | |
# | _ \| _| | |_ | | | | |_) | _| | |
# | |_) | |___| _|| |_| | _ <| |___ | |
# |____/|_____|_| \___/|_| \_\_____| | |
require 'test_helper' | |
class ProjectsControllerTest < ActionController::TestCase | |
context "on GET to :index" do | |
setup do | |
get :index | |
end | |
should_respond_with :success | |
should_assign_to :projects | |
end | |
context "on GET to :new" do | |
setup do | |
get :new | |
end | |
should_respond_with :success | |
end | |
# ... | |
context "Accessing a project" do | |
setup do | |
@project = Factory.create(:project) | |
end | |
context "on GET to :show" do | |
setup do | |
get :show, :id => @project | |
end | |
should_respond_with :success | |
end | |
context "on PUT to :update" do | |
setup do | |
put :update, { :id => @project, :project => {} } | |
end | |
should_redirect_to "project_path(@project)" | |
end | |
# ... | |
end | |
end |
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
# _ _____ _____ _____ ____ | |
# / \ | ___|_ _| ____| _ \ | |
# / _ \ | |_ | | | _| | |_) | | |
# / ___ \| _| | | | |___| _ < | |
# /_/ \_\_| |_| |_____|_| \_\ | |
require 'test_helper' | |
class ProjectsControllerTest < ActionController::TestCase | |
on_get :index do | |
should_respond_with :success | |
should_assign_to :projects | |
end | |
on_get :new do | |
should_respond_with :success | |
end | |
# ... | |
context "Accessing a project" do | |
setup do | |
@project = Factory.create(:project) | |
end | |
on_get :show, lambda { Hash[:id => @project] } do | |
should_respond_with :success | |
end | |
on_put :update, lambda { Hash[:id => @project, :project => {}] } do | |
should_redirect_to "project_path(@project)" | |
end | |
# ... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment