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

Michael Guterl mguterl at gmail.com
Sun Feb 26 17:02:36 UTC 2012


On Sun, Feb 26, 2012 at 11:36 AM, 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.
>

What about something like this?

def sign_up(attributes = {})
  attributes.reverse_merge!(:first_name => "First",
                            :last_name  => "Last",
                            :email => "foo at example.org",
                            :password => "password",
                            :password_confirmation => "password")

  fill_in "First Name", :with => attributes[:first_name]
  fill_in "Last Name", :with => attributes[:last_name]
  fill_in "Email Address", :with => attributes[:email]
  fill_in "Password", :with => attributes[:password]
  fill_in "Confirm Password", :with => attributes[:password_confirmation]
  click_on "Submit"
end

Then call it with different attributes based on your scenarios:

sign_up(:password_confirmation => "foo")
sign_up(:email => '')

Best,
Michael Guterl


More information about the rspec-users mailing list