Skip to content

Commit

Permalink
Add stub_template method for view specs.
Browse files Browse the repository at this point in the history
- supports stubbing partials rendered by views in view specs
- Closes #263.
  • Loading branch information
dchelimsky committed Nov 23, 2010
1 parent 5ae898e commit 55acd0f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
51 changes: 51 additions & 0 deletions features/view_specs/stub_template.feature
@@ -0,0 +1,51 @@
Feature: stub template

In order to isolate view specs from the partials rendered by the primary
view, rspec-rails (since 2.2) provides the stub_template method.

Scenario: stub template that does not exist
Given a file named "spec/views/gadgets/list.html.erb_spec.rb" with:
"""
require "spec_helper"
describe "gadgets/list.html.erb" do
it "renders the gadget partial for each gadget" do
assign(:gadgets, [
mock_model(Gadget, :id => 1, :name => "First"),
mock_model(Gadget, :id => 2, :name => "Second")
])
stub_template "gadgets/_gadget.html.erb" => "<%= gadget.name %><br/>"
render
rendered.should =~ /First/
rendered.should =~ /Second/
end
end
"""

And a file named "app/views/gadgets/list.html.erb" with:
"""
<%= render :partial => "gadget", :collection => @gadgets %>
"""
When I run "rspec spec/views/gadgets/list.html.erb_spec.rb"
Then the output should contain "1 example, 0 failures"

Scenario: stub template that exists
Given a file named "spec/views/gadgets/edit.html.erb_spec.rb" with:
"""
require "spec_helper"
describe "gadgets/edit.html.erb" do
before(:each) do
@gadget = assign(:gadget, stub_model(Gadget))
end
it "renders the form partial" do
stub_template "gadgets/_form.html.erb" => "This content"
render
rendered.should =~ /This content/
end
end
"""
When I run "rspec spec/views/gadgets/edit.html.erb_spec.rb"
Then the output should contain "1 example, 0 failures"

12 changes: 12 additions & 0 deletions lib/rspec/rails/example/view_example_group.rb
Expand Up @@ -85,6 +85,18 @@ def view
_view
end

# Simulates the presence of a template on the file system by adding a
# Rails' FixtureResolver to the front of the view_paths list. Designed to
# help isolate view examples from partials rendered by the view template
# that is the subject of the example.
#
# == Example
#
# stub_template("widgets/_widget.html.erb" => "This content.")
def stub_template(hash)
view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
end

# Provides access to the params hash that will be available within the
# view:
#
Expand Down

0 comments on commit 55acd0f

Please sign in to comment.