Created
October 31, 2008 17:32
-
-
Save dchelimsky/21354 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
module InstanceMethods | |
def should_render template | |
it "should render the #{template} template" do | |
do_request | |
response.should render_template(template) | |
end | |
end | |
def should_assign var_name | |
it "should assign #{var_name}" do | |
do_request | |
assigns(var_name).should == @mock_model | |
end | |
end | |
def expecting type, method, opts, &block | |
args = opts[:with] | |
before(:each) do | |
@mock_model = mock_model(type) do |m| | |
m.as_null_object | |
other_method = method == :create ? :new : :create | |
type.should_receive(method).with(args).and_return(m) | |
type.should_not_receive(other_method).with do | |
raise "#{type} is expecting #{method.inspect}, but it received #{other_method.inspect}" | |
end | |
end | |
end | |
instance_eval &block | |
end | |
end | |
def on request_type, action, *args, &block | |
describe "on #{request_type.to_s.upcase} #{action}" do | |
extend InstanceMethods | |
define_method :do_request do | |
__send__(request_type, action, *args) | |
end | |
instance_eval &block | |
end | |
end | |
end |
This file contains hidden or 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
describe ThingsController do | |
extend ControllerMacros | |
on :get, :index do | |
should_render "index" | |
end | |
on :post, :create, :thing => {:these => 'params'} do | |
expecting Thing, :to_receive => :new, :with => {'these' => 'params'} do | |
should_assign :thing | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment