Is there a way to validate head content in a view spec?
Thanks,
-Michael
Example
---
require "spec_helper"
describe "users/sessions/new.html.erb" do
describe "head" do
it "should have a head" do
render
rendered.should have_selector("head")
end
end
end
Failure/Error: rendered.should have_selector("head")
expected following output to contain a <head/> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>...
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users
The render method you see in a view spec delegates to the render method in ActionView::TestCase, which, in turn, delegates to view.render. Within rails, view.render is called usually by a controller, which provides all the necessary context information, like what layout to render. In this case, because you are specifying things that happen in a layout, you need to provide that context information in the spec:
render :template => "users/sessions, :layout => "layouts/application"
Note that you need to include ":template => ..." because RSpec's render method only sets the template if there are no arguments.
HTH,
David
Here is my Rails view with a custom method #area added, which uses
#with_output_buffer
class MyView < ActionView::Base
attr_accessor :current_user
def area(clazz, &block)
content = with_output_buffer(&block)
content_tag :div, content, :class => clazz
end
end
view = MyView.new
require 'erb'
x = 42
template = ERB.new <<-EOF
The value of x is: <%= my_area %>
EOF
puts template.result(binding)
my_area = view.area :mine do
'hello'
end
puts my_area
my_area.should match /hello/
=> <div class="mine"></div>
Yeah, it outputs the content_tag without 'hello' since it is output in
a separate buffer. Bot sure how I would get around this!?
And how to introduce this behavior into ERB?
I guess I should read up on basic rspec view testing instead of trying
to roll my own rspec framework for it!
How would I do this using rspe-rails for RSpec 2?
Thanks!
Kristian
class MyView < ActionView::Base
attr_accessor :current_user
def with_output_buffer
yield
end
end
describe EventsHelper do
describe "#link_to_event" do
it "displays the title, and formatted date" do
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
# helper is an instance of ActionView::Base configured with the
# EventsHelper and all of Rails' built-in helpers
helper.link_to_event.should =~ /Ruby Kaigi, 27 Aug, 2010/
end
end
end
So helper is an instance of ActionView::Base. Very useful... I think.
But how do I use it for my scenario I wonder?
-Michael