I have a Rails spec where I want to check that the action_name is either "create" or "update". I can think of a couple of ways to do it, but none of them reads fantastically well: 1. ["create", "update"].should include(controller.action_name) Problem: The error message, should it fail, is: expected ["create", "update"] to include "foo" 2. controller.action_name.should match(/^create|update$/) Problem: Not brilliantly readable, but the error message would be better. Is there a better way to do it? Something like this would be cool: controller.action_name.should == any_of("create", "update") Jon
On Thu, Jul 17, 2008 at 6:18 AM, Jonathan Leighton <j at jonathanleighton.com> wrote:> I have a Rails spec where I want to check that the action_name is either > "create" or "update". I can think of a couple of ways to do it, but none > of them reads fantastically well: > > 1. ["create", "update"].should include(controller.action_name) > > Problem: The error message, should it fail, is: expected ["create", > "update"] to include "foo" > > 2. controller.action_name.should match(/^create|update$/) > > Problem: Not brilliantly readable, but the error message would be > better. > > Is there a better way to do it? Something like this would be cool: > > controller.action_name.should == any_of("create", "update")Hi Jon - There''s not really anything more clear supported, but you could always write a custom matcher for this. I''m wondering why the examples aren''t more specific though. Why is it OK that the action could be one of two possibilities given a specific set of givens? David> Jon
On Thu, 2008-07-17 at 07:28 -0500, David Chelimsky wrote:> I''m wondering why the examples aren''t more specific though. Why is it > OK that the action could be one of two possibilities given a specific > set of givens?Ok well I lied a little bit :) I am using it in a story step, specifically the step is "Then the form should be shown". I guess I could split them into ?"Then the new/edit form should be shown" but it doesn''t seem a huge issue... -- Jonathan Leighton http://jonathanleighton.com/
On Jul 17, 2008, at 7:40 AM, Jonathan Leighton wrote:> On Thu, 2008-07-17 at 07:28 -0500, David Chelimsky wrote: >> I''m wondering why the examples aren''t more specific though. Why is it >> OK that the action could be one of two possibilities given a specific >> set of givens? > > Ok well I lied a little bit :) > > I am using it in a story step, specifically the step is "Then the form > should be shown". I guess I could split them into "Then the new/edit > form should be shown" but it doesn''t seem a huge issue...Aha. Now that''s a horse of a different color. I tend to avoid details like that in story steps. I prefer to expect what''s visible, submit forms (whatever the action is) and expect to end up in the right place. Are you familiar with Webrat? It''s a tool that allows you to describe things at a much higher level and takes care of the low level detail for you. So rather than expecting specific form elements in the story steps, you just do things like this: Given I am registered as David with password Secret And am and Administrator When I log in with David/Secret Then I should see Manage Schedules in a list of Things To Do The interesting step here is "When I sign in with David/Secret": When /I log in with (.*)\/(.*)/ do |login, password| visits "/login" fills_in "Login", :with => login fills_in "Password", :with => password clicks_button "Log In" end The fills_in method does two things at once: expects to find an item with either an id of "Login" or with a related form label that has the text "Login". Then it manipulates the DOM, setting the value of that element to "David" (in this example). Then #clicks_button finds a button with the text "Log In", builds a POST from the related DOM elements and submits the POST. I use this 100% of the time for Rails stories these days and am overall very happy with the resulting code. FWIW, Cheers, David> -- > Jonathan Leighton > http://jonathanleighton.com/
On Thu, 2008-07-17 at 08:50 -0500, David Chelimsky wrote:> Are you familiar with Webrat?Yep, we are using it. However, I want to test what happens when invalid data is entered - that''s the reason to specify that the form gets shown. What would you consider a better approach to verify the user sees the form again, instead of moving on through the application? I guess I could test that they *don''t* get redirected? Cheers, Jon -- Jonathan Leighton http://jonathanleighton.com/
Perhaps... When "I login with invalid credentials" Then "I see that I have not been logged in" And in your implementation of the Then you could make sure they are at the login form still. Zach On Thu, Jul 17, 2008 at 10:19 AM, Jonathan Leighton <j at jonathanleighton.com> wrote:> On Thu, 2008-07-17 at 08:50 -0500, David Chelimsky wrote: >> Are you familiar with Webrat? > > Yep, we are using it. However, I want to test what happens when invalid > data is entered - that''s the reason to specify that the form gets shown. > What would you consider a better approach to verify the user sees the > form again, instead of moving on through the application? I guess I > could test that they *don''t* get redirected? > > Cheers, > Jon > > -- > Jonathan Leighton > http://jonathanleighton.com/ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
On Thu, Jul 17, 2008 at 9:37 AM, Zach Dennis <zach.dennis at gmail.com> wrote:> Perhaps... > > When "I login with invalid credentials" > Then "I see that I have not been logged in"Or .... When I login with invalid credentials Then I should see the login form And I should see a message saying "Something went wrong."> > And in your implementation of the Then you could make sure they are at > the login form still. > > Zach > > > > > On Thu, Jul 17, 2008 at 10:19 AM, Jonathan Leighton > <j at jonathanleighton.com> wrote: >> On Thu, 2008-07-17 at 08:50 -0500, David Chelimsky wrote: >>> Are you familiar with Webrat? >> >> Yep, we are using it. However, I want to test what happens when invalid >> data is entered - that''s the reason to specify that the form gets shown. >> What would you consider a better approach to verify the user sees the >> form again, instead of moving on through the application? I guess I >> could test that they *don''t* get redirected? >> >> Cheers, >> Jon >> >> -- >> Jonathan Leighton >> http://jonathanleighton.com/ >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > Zach Dennis > http://www.continuousthinking.com > http://www.mutuallyhuman.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Thu, 2008-07-17 at 09:40 -0500, David Chelimsky wrote:> On Thu, Jul 17, 2008 at 9:37 AM, Zach Dennis <zach.dennis at gmail.com> wrote: > > Perhaps... > > > > When "I login with invalid credentials" > > Then "I see that I have not been logged in" > > Or .... > > When I login with invalid credentials > Then I should see the login form > And I should see a message saying "Something went wrong."What would be the implementation of "?Then I should see the login form"? We are already testing for an error message :) Jon
On Thu, Jul 17, 2008 at 11:04 AM, Jonathan Leighton <j at jonathanleighton.com> wrote:> On Thu, 2008-07-17 at 09:40 -0500, David Chelimsky wrote: >> On Thu, Jul 17, 2008 at 9:37 AM, Zach Dennis <zach.dennis at gmail.com> wrote: >> > Perhaps... >> > >> > When "I login with invalid credentials" >> > Then "I see that I have not been logged in" >> >> Or .... >> >> When I login with invalid credentials >> Then I should see the login form >> And I should see a message saying "Something went wrong." > > What would be the implementation of "?Then I should see the login form"? > We are already testing for an error message :)As with all things, context is everything. *If* I were going to have such a step, I''d do it based on things in the form: Then "should see the login form" do response.should have_tag(''input#login'') response.should have_tag(''input#password'') end That''s more granular than I generally like, but that''s what probably what I''d do in this scenario. HTH, David> > Jon > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 18-jul-2008, at 1:22, David Chelimsky wrote:> On Thu, Jul 17, 2008 at 11:04 AM, Jonathan Leighton > <j at jonathanleighton.com> wrote: >> On Thu, 2008-07-17 at 09:40 -0500, David Chelimsky wrote: >>> On Thu, Jul 17, 2008 at 9:37 AM, Zach Dennis >>> <zach.dennis at gmail.com> wrote: >>>> Perhaps... >>>> >>>> When "I login with invalid credentials" >>>> Then "I see that I have not been logged in" >>> >>> Or .... >>> >>> When I login with invalid credentials >>> Then I should see the login form >>> And I should see a message saying "Something went wrong." >> >> What would be the implementation of "Then I should see the login >> form"? >> We are already testing for an error message :) > > As with all things, context is everything.Definately. For what it''s worth, my approach: I use the "should see the form | page" construct a lot when implementing new functionality. I expect a form with just response.should have_tag(''form[action=?]'', blogposts_path), and spec the form fields in my view specs. This ensures that my integration tests won''t necessarily break when I change a form field, but it does get picked up by the view specs. After I''m done prototyping, I write higher level stories dealing with the flow of the functionality. gr, bartz> > *If* I were going to have such a step, I''d do it based on things in > the form: > > Then "should see the login form" do > response.should have_tag(''input#login'') > response.should have_tag(''input#password'') > end > > That''s more granular than I generally like, but that''s what probably > what I''d do in this scenario.