Hello, I''m new in Rspec and I would like to ask a question. Suppose I have a test that fails raising an exception. I do not want the test to raise an exception and I''m not expecting one, but something fails and an exception occurs. Is there some way to handle that exception and do something in that case? I was thinking of some way of checking that a test failed in *after(:each)*method and handle the error there (I want to clear some variables and re-start others). Is that possible? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/11b5fa66/attachment.html>
On Jun 30, 2010, at 8:45 AM, Marcos Chicote wrote:> Hello, > I''m new in Rspec and I would like to ask a question. > Suppose I have a test that fails raising an exception. I do not want the test to raise an exception and I''m not expecting one, but something fails and an exception occurs. > Is there some way to handle that exception and do something in that case? > > I was thinking of some way of checking that a test failed in after(:each) method and handle the error there (I want to clear some variables and re-start others). > > Is that possible?Exceptions in before/after(:all) will bubble up as exceptions, but exceptions in before/after(:each) or in the examples are handled and treated as failures. Are you experiencing something different? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/ee425bb2/attachment.html>
I don''t think so, but I don''t really know how to check it programatically. I don''t mean exceptions in *before/after *methos, but inside *it()* method I would like to write something like this: after(:each) do if exception_occured_on_it_method? do_something end end Is that possible? On Wed, Jun 30, 2010 at 10:58 AM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Jun 30, 2010, at 8:45 AM, Marcos Chicote wrote: > > Hello, > I''m new in Rspec and I would like to ask a question. > Suppose I have a test that fails raising an exception. I do not want the > test to raise an exception and I''m not expecting one, but something fails > and an exception occurs. > Is there some way to handle that exception and do something in that case? > > I was thinking of some way of checking that a test failed in *after(:each) > * method and handle the error there (I want to clear some variables and > re-start others). > > Is that possible? > > > Exceptions in before/after(:all) will bubble up as exceptions, but > exceptions in before/after(:each) or in the examples are handled and treated > as failures. Are you experiencing something different? > > _______________________________________________ > 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/20100630/1bd1be69/attachment.html>
On Jun 30, 2010, at 9:05 AM, Marcos Chicote <totochicote at gmail.com> wrote:> I don''t think so, but I don''t really know how to check it > programatically. > I don''t mean exceptions in before/after methos, but inside it() method > > I would like to write something like this: > after(:each) do > if exception_occured_on_it_method? > do_something > end > end > > Is that possible?What problem are you trying to solve?> > On Wed, Jun 30, 2010 at 10:58 AM, David Chelimsky <dchelimsky at gmail.com > > wrote: > On Jun 30, 2010, at 8:45 AM, Marcos Chicote wrote: > >> Hello, >> I''m new in Rspec and I would like to ask a question. >> Suppose I have a test that fails raising an exception. I do not >> want the test to raise an exception and I''m not expecting one, but >> something fails and an exception occurs. >> Is there some way to handle that exception and do something in that >> case? >> >> I was thinking of some way of checking that a test failed in after >> (:each) method and handle the error there (I want to clear some >> variables and re-start others). >> >> Is that possible? > > Exceptions in before/after(:all) will bubble up as exceptions, but > exceptions in before/after(:each) or in the examples are handled and > treated as failures. Are you experiencing something different? > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > 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/20100630/896f5e96/attachment-0001.html>
On Wed, Jun 30, 2010 at 11:12 AM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Jun 30, 2010, at 9:05 AM, Marcos Chicote <totochicote at gmail.com> wrote: > > I don''t think so, but I don''t really know how to check it programatically. > I don''t mean exceptions in *before/after *methos, but inside *it()* method > > I would like to write something like this: > after(:each) do > if exception_occured_on_it_method? > do_something > end > end > > Is that possible? > > > What problem are you trying to solve? >I''m using rspec to build tests using Watir. On *before(:each)*, I create the browser instance and login lazily to a web page (if the browser exists, I use that instance, if it does not, I create a new one). I''m using this lazy approach in order to save the time of openning a new browser and login for each test (I could close the browser on * after(:each)* but this is faster). I most cases (when test are passed), everything works great. The problem is that sometimes the page I''m trying to access doesn''t load (or there is some other non functional problem), the browser keeps wating and an timer that I implemented timesout.This timeout raises an exception that makes the test fail, but does not close the browsers windows (that keeps wating for the response), making following tests to fail. If I could handle timeout exception in the way I posted before, I could close the browser and the next test will open a fresh one. (Note: this is only and example, there are some other exceptions thay might occur, html element missing for example, that I want to handle the same way and that''s why I need a unified mechanism) Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/c93f796b/attachment.html>
On Jun 30, 2010, at 9:29 AM, Marcos Chicote wrote:> On Wed, Jun 30, 2010 at 11:12 AM, David Chelimsky <dchelimsky at gmail.com> wrote: > On Jun 30, 2010, at 9:05 AM, Marcos Chicote <totochicote at gmail.com> wrote: > >> I don''t think so, but I don''t really know how to check it programatically. >> I don''t mean exceptions in before/after methos, but inside it() method >> >> I would like to write something like this: >> after(:each) do >> if exception_occured_on_it_method? >> do_something >> end >> end >> >> Is that possible? > > What problem are you trying to solve? > > I''m using rspec to build tests using Watir. > > On before(:each), I create the browser instance and login lazily to a web page (if the browser exists, I use that instance, if it does not, I create a new one). I''m using this lazy approach in order to save the time of openning a new browser and login for each test (I could close the browser on after(:each) but this is faster). > > I most cases (when test are passed), everything works great. The problem is that sometimes the page I''m trying to access doesn''t load (or there is some other non functional problem), the browser keeps wating and an timer that I implemented timesout.This timeout raises an exception that makes the test fail, but does not close the browsers windows (that keeps wating for the response), making following tests to fail. > > If I could handle timeout exception in the way I posted before, I could close the browser and the next test will open a fresh one. > > (Note: this is only and example, there are some other exceptions thay might occur, html element missing for example, that I want to handle the same way and that''s why I need a unified mechanism)There''s nothing in RSpec to explicitly handle this for you. There are tools that will likely be available in RSpec-2 by the time we do a final release, but they won''t work yet for your goal, so for the short run I think you need to manage this in each example manually. I''d recommend something like: def capture(exception) begin yield rescue Exception => e case e if OneType # do something elsif AnotherType # do something different end end end it "..." do capture do # do stuff end end It''s not perfect, but it should work. HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/d218dc4f/attachment.html>
On Wed, Jun 30, 2010 at 12:31 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Jun 30, 2010, at 9:29 AM, Marcos Chicote wrote: > > On Wed, Jun 30, 2010 at 11:12 AM, David Chelimsky <dchelimsky at gmail.com>wrote: > >> On Jun 30, 2010, at 9:05 AM, Marcos Chicote <totochicote at gmail.com> >> wrote: >> >> I don''t think so, but I don''t really know how to check it programatically. >> I don''t mean exceptions in *before/after *methos, but inside *it()*method >> >> I would like to write something like this: >> after(:each) do >> if exception_occured_on_it_method? >> do_something >> end >> end >> >> Is that possible? >> >> >> What problem are you trying to solve? >> > > I''m using rspec to build tests using Watir. > > On *before(:each)*, I create the browser instance and login lazily to a > web page (if the browser exists, I use that instance, if it does not, I > create a new one). I''m using this lazy approach in order to save the time of > openning a new browser and login for each test (I could close the browser on > *after(:each)* but this is faster). > > I most cases (when test are passed), everything works great. The problem is > that sometimes the page I''m trying to access doesn''t load (or there is some > other non functional problem), the browser keeps wating and an timer that I > implemented timesout.This timeout raises an exception that makes the test > fail, but does not close the browsers windows (that keeps wating for the > response), making following tests to fail. > > If I could handle timeout exception in the way I posted before, I could > close the browser and the next test will open a fresh one. > > (Note: this is only and example, there are some other exceptions thay might > occur, html element missing for example, that I want to handle the same way > and that''s why I need a unified mechanism) > > > There''s nothing in RSpec to explicitly handle this for you. There are tools > that will likely be available in RSpec-2 by the time we do a final release, > but they won''t work yet for your goal, so for the short run I think you need > to manage this in each example manually. I''d recommend something like: > > def capture(exception) > begin > yield > rescue Exception => e > case e > if OneType > # do something > elsif AnotherType > # do something different > end > end > end > > it "..." do > capture do > # do stuff > end > end > > It''s not perfect, but it should work. > > HTH, > David > >Thanks David. I''m looking forward to RSpec-2. Is there an estimate on the release date? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/efa73870/attachment.html>
On Jun 30, 2010, at 10:46 AM, Marcos Chicote wrote:> On Wed, Jun 30, 2010 at 12:31 PM, David Chelimsky <dchelimsky at gmail.com> wrote: > On Jun 30, 2010, at 9:29 AM, Marcos Chicote wrote: > >> On Wed, Jun 30, 2010 at 11:12 AM, David Chelimsky <dchelimsky at gmail.com> wrote: >> On Jun 30, 2010, at 9:05 AM, Marcos Chicote <totochicote at gmail.com> wrote: >> >>> I don''t think so, but I don''t really know how to check it programatically. >>> I don''t mean exceptions in before/after methos, but inside it() method >>> >>> I would like to write something like this: >>> after(:each) do >>> if exception_occured_on_it_method? >>> do_something >>> end >>> end >>> >>> Is that possible? >> >> What problem are you trying to solve? >> >> I''m using rspec to build tests using Watir. >> >> On before(:each), I create the browser instance and login lazily to a web page (if the browser exists, I use that instance, if it does not, I create a new one). I''m using this lazy approach in order to save the time of openning a new browser and login for each test (I could close the browser on after(:each) but this is faster). >> >> I most cases (when test are passed), everything works great. The problem is that sometimes the page I''m trying to access doesn''t load (or there is some other non functional problem), the browser keeps wating and an timer that I implemented timesout.This timeout raises an exception that makes the test fail, but does not close the browsers windows (that keeps wating for the response), making following tests to fail. >> >> If I could handle timeout exception in the way I posted before, I could close the browser and the next test will open a fresh one. >> >> (Note: this is only and example, there are some other exceptions thay might occur, html element missing for example, that I want to handle the same way and that''s why I need a unified mechanism) > > There''s nothing in RSpec to explicitly handle this for you. There are tools that will likely be available in RSpec-2 by the time we do a final release, but they won''t work yet for your goal, so for the short run I think you need to manage this in each example manually. I''d recommend something like: > > def capture(exception) > begin > yield > rescue Exception => e > case e > if OneType > # do something > elsif AnotherType > # do something different > end > end > end > > it "..." do > capture do > # do stuff > end > end > > It''s not perfect, but it should work. > > HTH, > David > > > Thanks David. I''m looking forward to RSpec-2. Is there an estimate on the release date?It''s been in beta for a while - I released beta.15 this morning. Planning to do an RC sometime in July, at which point it will be feature complete and we''ll just do bug fixes and documentation enhancements for the final release. Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100630/51af46b8/attachment-0001.html>