How to test an exception notifier with cucumber ?

1,001 views
Skip to first unread message

Olivier F

unread,
Aug 5, 2009, 8:46:13 PM8/5/09
to Cukes
Hi,

I have installed an exception notifier plugin for Rails (http://
github.com/rails/exception_notification/tree/master) and I would like
to test it with cucumber.

So I made a feature file like this :
Background:
Given Emails aren't really sent for now
And I know the size of deliveries array

Scenario: Send an error
Given I go to the dummy_method_that_raises_exception
Then An e-mail should have been sent

And a step file :
Given /^Emails aren't really sent for now$/ do
ActionMailer::Base.delivery_method = :test
end

Given /^I know the size of deliveries array$/ do
@size_before = ActionMailer::Base.deliveries.count
end

Then /^An e\-mail should have been sent$/ do
ActionMailer::Base.deliveries.count.should eql( @size_before + 1 )
end

Also, in path.rb, I added these lines :
when /the dummy_method_that_raises_exception/
"/mycontroller/dummy_method_that_raises_exception"

Finally, in mycontroller I made the dummy_method_that_raises_exception
method like this:
def dummy_method_that_raises_exception
raise "Critical error"
end

My problem is that when I run the tests with cucumber, I obviously get
this error :
Given I go to the dummy_method_that_raises_exception # features/
step_definitions/webrat_steps.rb:10
Critical error (RuntimeError)

So I would like to raise this error in order to actually test my
notifier, but I would also like to say to cucumber that this error is
expected. I want to raise the error for the application, not for
cucumber.

Could you help me ?

Thank you
-Olivier

Perryn Fowler

unread,
Aug 6, 2009, 3:42:52 AM8/6/09
to cu...@googlegroups.com
could you not implement your own "Given an exception is raised" step
that delegates to "Given I go to .. " and rescues the exception?
--
-----------------------
Perryn Fowler
ThoughtWorks

nruth

unread,
Aug 6, 2009, 5:19:59 AM8/6/09
to Cukes
I use the following steps, which may be good examples of what Perryn
suggests

Then /^when I go to the (.+? page) permission should be denied$/ do |
page|
lambda {When "I go to the #{page}"}.should raise_error(AccessDenied)
end

and more generally, using one of your other when steps

Then /^when (.*) an error should occur$/ do |action|
lambda {When action}.should raise_error
end

Regards

Nick

Olivier F

unread,
Aug 7, 2009, 2:38:28 PM8/7/09
to Cukes
Thanks to all.

Now I don't get the error anymore, I mean the step passes
successfully.
But I have another issue which is a little bit weird : the step just
after the step that raises the error is skipped.
Background
Given Emails aren't really sent for now <-----------------
success
And I know the size of deliveries array <-----------------
success

Scenario: Send an error
Then I raise a critical error <----------------- success
And An e-mail should have been sent <----------------- this one
is SKIPED !!!!

Here is my "I raise a critical error" step :
Given /^I raise a critical error$/ do
lambda {When "I go to the dummy_method_raises_exception"}.should
raise_error(RuntimeError)
end


Also, as the next step is skipped, I tried to add the next step
content to this step, so I have:
Given /^I raise a critical error$/ do
lambda {When "I go to the dummy_method_raises_exception"}.should
raise_error(RuntimeError)
ActionMailer::Base.deliveries.count.should eql( @size_before + 1 )
end

And if I do that the step fails because it expected 1 and got 0. Maybe
I've not installed the notifier correctly, but couldn't it be because
cucumber catches the exception ? (and so, as the exception is caught,
the notifier doesn't send anything)


Have you got any idea or even better a solution for one of these two
problems ?

Thank you!
-Olivier

Matt Wynne

unread,
Aug 9, 2009, 7:22:25 AM8/9/09
to cu...@googlegroups.com
Try just calling #visit "/mycontroller/
dummy_method_that_raises_exception" in the lamba instead of calling
the Cucumber step - that way there won't be any failed steps.

Also watch out for whether you're calling
Cucumber::Rails.bypass_rescue anywhere in the env.rb code that this
feature runs in. If you want the error to be raised into the features,
you probably do want to call bypass_rescue, but you may find it causes
the rails error handling to behave a bit differently to how it would
in production.

>
> >

cheers,
Matt

+447974 430184
ma...@mattwynne.net
http://mattwynne.net

Reply all
Reply to author
Forward
0 new messages