[rspec-users] requests: is it possible to put the fill_in's into a method?

Andrew Premdas apremdas at gmail.com
Sun Feb 26 17:25:51 UTC 2012


On 26 February 2012 16:36, S Ahmed <sahmed1020 at gmail.com> wrote:

> I'm testing my signup page, and I want to minimize the duplication of the
> fill_in code for filling in the form fields and testing how my page reacts
> when someone forgets to enter input.
>
> fill_in "....", with: "abc123"
>
> Any tricks of doing this?
>
> Say I have 10 fill_in calls, so say I want to test to make sure the form
> fails if any combination of the last 4 fields are missing.
>
> I was thinking of putting the first 6 fill_in calls into a method, and
> then calling that method:
>
> it "should ..." do
>   enter_first_6
>   # now enter 3 of the 4 and verify
> end
>
> it "should ..." do
>   enter_first_6
>   # now enter a different combination of the last 4 fields
> end
>
> I haven't tested this yet, just brainstorming, any other advise?
>
> I wish things worked liked attribute hashes where you could just call
> .merge and change the default set.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


Quite a good way to do this is to create a hash that has all your values
and then modify the hash for your errors. So you could do something like

def good_attrs()
  {
     :foo => 'foo'
     :bar => 'bar
     ...
  }
end

then have a fill in method that takes the hash

def fill_in(attrs)
  attrs.each do |k,v|
    fill_in k, :with => v
    ...

finally do your errors by

fill_in(good_attrs(:with => {:foo => nil})

or use :except etc.

You can also put your a spec inside a loop

%w(foo bar bax).each do |bad_attr|
   it "should ... #{bad_attr}..." do
     fill_in(good_attrs(:except => {:bad_attr})

etc.

All of top of my head so expect syntax errors, but hopefully enough to be
useful

All best

Andrew

HTH

Andrew
-- 
------------------------
Andrew Premdas
blog.andrew.premdas.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120226/3958680d/attachment.html>


More information about the rspec-users mailing list