[Rspec-devel] Some handy rails helpers

Jay Levitt lists-rspec at shopwatch.org
Tue Jul 4 09:16:07 EDT 2006


Luke Redpath wrote:
> 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')

Oh, I like that a lot!  And your improved syntax for the 
should_have_fields helper, too.

Unfortunately, I don't know nearly enough Ruby metaprogramming to do 
them - at least not yet - but perhaps someone else will step up in the 
mean time...  Otherwise, ping me in a few months and we'll see what I've 
learned.

Jay
> 
> 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 <mailto: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 <mailto:Rspec-devel at rubyforge.org>
>> http://rubyforge.org/mailman/listinfo/rspec-devel
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel



More information about the Rspec-devel mailing list