S Ahmed
2012-Feb-26 16:36 UTC
[rspec-users] requests: is it possible to put the fill_in''s into a method?
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120226/fbd7260c/attachment.html>
Michael Guterl
2012-Feb-26 17:02 UTC
[rspec-users] requests: is it possible to put the fill_in''s into a method?
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
Andrew Premdas
2012-Feb-26 17:25 UTC
[rspec-users] requests: is it possible to put the fill_in''s into a method?
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>
S Ahmed
2012-Feb-26 22:16 UTC
[rspec-users] requests: is it possible to put the fill_in''s into a method?
ahh, nice makes sense thanks! On Sun, Feb 26, 2012 at 12:25 PM, Andrew Premdas <apremdas at gmail.com> wrote:> 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 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120226/e5ec39e8/attachment.html>