[rspec-users] First Post (Best Practices)

Matt Wynne matt at mattwynne.net
Thu May 13 18:45:16 EDT 2010


On 13 May 2010, at 12:41, Ants Pants wrote:

> Hello everyone,

Hello and welcome :)

> I'm just working my way through the RSpec/Cucumber book, I love the tool but not so much the learning curve :(
> 
> Starting with my first view, is it okay for me, in the BDD world, to do the following or can I put them all in one 'it' block to save on having to render the page every time.

Don't worry about performance here - it's negligible. In general I always err towards the idea of 'one assertion per test', so that when I get a failing test (or 'example' as we call them in RSpec) I know exactly what has gone broken.

> Or must they be in separate blocks with separate renders? Or is there a fourth more elegant solution?

Some people don't bother testing views in this much detail, and just rely on Cucumber tests to tell them whether the page works as expected. On the other hand you might well find that this level of detail helps you to think about the design of the objects that the view uses.

If you search this list for previous threads about 'should I spec views' you will find plenty has already been said on this topic. 

> 
>   %w{ name address_1 address_2 address_3 postcode town_city state country email phone www }.each do |field|
>     it "renders form field #{field} to create a new competition" do
>       @competition.stub!(field.intern).and_return ''
>       render "competitions/new.html.erb"
>       response.should have_selector("input[type=text]", :name => "competition[#{field}]", :value => '') 
>     end
>   end
> 
> Is it okay to post this type of  question if nothing jumps out at me after googling?

Of course but remember we're all busy people and not getting paid for this - remember to ask the plants[1] in the office before you ask us!

> 
> Thanks
> 
> ants
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

[1]http://www.cb1.com/~john/computing/rubber-plant-effect.html



More information about the rspec-users mailing list