Permalink
Browse files

add some RSpec code examples for render() in view specs

  • Loading branch information...
dchelimsky committed May 22, 2010
1 parent 0ea6030 commit 1e6ab79deae80b217b152c81caf938127dd54a24
Showing with 67 additions and 4 deletions.
  1. +24 −4 lib/rspec/rails/example/view_example_group.rb
  2. +43 −0 spec/rspec/rails/example/view_example_group_spec.rb
@@ -11,7 +11,7 @@ class ViewExampleController < ActionController::Base
module ViewExtension
def protect_against_forgery?; end
def method_missing(name, *args)
if controller.respond_to?(name) || Rails.application.routes.named_routes.helpers.include?(name)
if controller.respond_to?(name) || helpers.include?(name)
controller.__send__(name, *args)
else
super(name, *args)
@@ -50,13 +50,33 @@ def controller_path
parts.join('/')
end
# :callseq:
# render
# render("some/path")
# render(:template => "some/path")
# render({:partial => "some/path"}, {... locals ...})
# render({:partial => "some/path"}, {... locals ...}) do ... end
def render(options = nil, locals = {}, &block)
options ||= {:file => file_to_render}
@response = view.render(options, locals, &block)
@response = view.render(prepare(options), locals, &block)
end
def prepare(options) # :nodoc:
case options
when String
{:file => options}
when nil
{:file => file_to_render}
else
options
end
end
def helpers
::Rails.application.routes.named_routes.helpers
end
def method_missing(selector, *args)
if ::Rails.application.routes.named_routes.helpers.include?(selector)
if helpers.include?(selector)
controller.__send__(selector, *args)
else
super
@@ -8,4 +8,47 @@
group = RSpec::Core::ExampleGroup.describe
group.included_modules.should include(ViewExampleGroupBehaviour)
end
describe "#render" do
let(:view_spec) do
Class.new do
include ViewExampleGroupBehaviour
end.new
end
let(:helpers) { double("helpers").as_null_object }
let(:example) { double("example").as_null_object }
before do
helpers.stub(:include?) { false }
view_spec.stub(:helpers) { helpers }
view_spec.stub(:running_example) { example }
end
context "given no input" do
it "sends render(:file => (described file)) to the view" do
view_spec.stub(:file_to_render) { "widgets/new.html.erb" }
view_spec.view.should_receive(:render).
with({:file => "widgets/new.html.erb"}, {})
view_spec.render
end
end
context "given a string" do
it "sends :file => string as the first arg to render" do
view_spec.view.should_receive(:render).
with({:file => 'arbitrary/path'}, {})
view_spec.render('arbitrary/path')
end
end
context "given a hash" do
it "sends the hash as the first arg to render" do
pending
view_spec.view.should_receive(:render).
with({:foo => 'bar'}, {})
view_spec.render(:foo => 'bar')
end
end
end
end

0 comments on commit 1e6ab79

Please sign in to comment.