Created
January 13, 2009 05:07
-
-
Save rmm5t/46335 to your computer and use it in GitHub Desktop.
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 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 = {}, &block) | |
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 = convert_hash_values_to_instance_variable_params(options) | |
send(method, action, opts) | |
end | |
merge_block(&block) | |
end | |
end | |
end | |
module ControllerHelpers | |
def convert_hash_values_to_instance_variable_params(hash) | |
converted = {} | |
hash.each do |k, v| | |
if v.is_a?(Hash) | |
v = convert_hash_values_to_instance_variable_params(v) | |
elsif v.to_s.starts_with?("@") | |
v = instance_eval(v.to_s) | |
end | |
converted[k] = v | |
end | |
converted | |
end | |
end | |
class Test::Unit::TestCase | |
extend ControllerMacros | |
include ControllerHelpers | |
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 :index, :format => "xml" do | |
should_respond_with :success | |
should_assign_to :projects | |
end | |
on_get :new do | |
should_respond_with :success | |
end | |
context "Given a client" do | |
setup do | |
@client = Factory.create(:client) | |
end | |
on_post :create, :project => Factory.attributes_for(:project, :client_id => "@client") do | |
should_change "Project.count", :by => 1 | |
should_redirect_to "project_path(@project)" | |
end | |
end | |
context "Accessing a project" do | |
setup do | |
@project = Factory.create(:project) | |
end | |
on_get :show, :id => "@project" do | |
should_respond_with :success | |
end | |
on_get :edit, :id => "@project.to_param" do | |
should_respond_with :success | |
end | |
on_put :update, :id => :@project, :project => {} do | |
should_redirect_to "project_path(@project)" | |
end | |
on_delete :destroy, :id => "@project" do | |
should_change "Project.count", :by => -1 | |
should_redirect_to "projects_path" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment