[Rspec-devel] Some handy rails helpers

Luke Redpath luke at agileevolved.com
Mon Jul 3 05:35:07 EDT 2006


Hi Jay

These look really cool, and your post got my thinking. I've always  
hated how clunky the assert_tag function that come with Rails is.  
Wouldn't it be really cool to have a DSL that merges in with rSpec  
for testing your HTML output - kind of like integration tests but I  
was thinking more about speccing particular controller actions (under  
their various contexts) and their resulting views. Something like  
this would be really cool:

tag('h1', :id => 'pageheader').should_contain('My Great Website')

Or even better:

h1(:id => 'pageheader').should_contain('My Great Website')
div(:id => 'somediv').should_have_class_name('hidden')

And based on your helpers:

form(:id => 'myform').should_have_fields("name", "email", "username",  
"passsword")

Luke Redpath
Senior Software Engineer

Agile Evolved Limited
E. luke at agileevolved.com
W. www.agileevolved.com


On 3 Jul 2006, at 03:49, Jay Levitt wrote:

> I've been using these in spec_helper:
>
> class Spec::Runner::ExecutionContext
>    def form_should_have_fields(obj, fields)
>      for field in fields
>        response.body.should_have_tag :tag => "input",
>          :attributes => { :name => "#{obj}[#{field}]" }
>      end
>    end
> end
>
> class Spec::Runner::Context
>    def fields_should_start_empty(obj, *fields)
>      fields.flatten.each do |f|
>        specify "should have an empty #{f} field" do
>          eval(obj.id2name + ".#{f}.should.be.nil")
>        end
>      end
>    end
>
>    def fields_cant_stay_empty(obj, *fields)
>      fields.flatten.each do |f|
>        specify "should disallow an empty #{f} field" do
>          o = eval(obj.id2name)
>          o.send("#{f}=", nil)
>          o.should.not.be.valid
>          o.errors.on(f).should.equal "can't be blank"
>        end
>      end
>    end
> end
>
> ---
>
> Usage:
>
> in a controller_spec.rb:
>
>    specify "get the new receipt form" do
>      get 'new'
>      response.should.be.success
>      response.should_render 'new'
>      form_should_have_fields("receipt", ["vendor", "purchased_at"])
>    end
>
> in a model spec.rb, where :valid is a fixture for a valid receipt:
>
> context "A newly created receipt" do
>    setup do
>      @receipt = Receipt.new
>    end
>
>    fields_should_start_empty(:@receipt, :vendor, :purchased_at)
> end
>
> context "An otherwise valid receipt" do
>    fixtures :receipts
>
>    setup do
>      @receipt = receipts(:valid)
>    end
>
>    specify "should start out valid" do
>      @receipt.should.be.valid
>    end
>
>    fields_cant_stay_empty(:@receipt, :vendor, :purchased_at)
> end
>
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20060703/3cbf5563/attachment.html 


More information about the Rspec-devel mailing list