[cucumber] Setting a constant in step definition


#1

Currently I have a very simple constants implementation being loaded as
a
Rails initialiser

module MVOR
module Postage
THRESHOLD = BigDecimal.new(‘6.99’)
RATE = BigDecimal.new(‘30.00’)
end
end

My scenarios of my postage feature want to deal with two situations when
the
THRESHOLD has been set to zero and to ‘6.99’. The question is how can I
set
the threshold in my step_definition. So I want to implement

Given the postage threshold is 0.00
Given the postage threshold is 6.99

I’ve considered a number of ideas, but seem to keep on getting into
complex
solutions for what I assume is something very simple.

TIA


#2

On Mon, Apr 27, 2009 at 2:00 PM, Andrew P. removed_email_address@domain.invalid
wrote:

My scenarios of my postage feature want to deal with two situations when
the THRESHOLD has been set to zero and to ‘6.99’. The question is how can I
set the threshold in my step_definition. So I want to implement

Given the postage threshold is 0.00
Given the postage threshold is 6.99

Since it’s a constant - how can it possibly change? And therefore - why
are
you testing it with different values?


#3

On Mon, Apr 27, 2009 at 7:00 AM, Andrew P. removed_email_address@domain.invalid
wrote:

My scenarios of my postage feature want to deal with two situations when the
THRESHOLD has been set to zero and to ‘6.99’. The question is how can I set
the threshold in my step_definition. So I want to implement

Given the postage threshold is 0.00
Given the postage threshold is 6.99

I’ve considered a number of ideas, but seem to keep on getting into complex
solutions for what I assume is something very simple.

If THRESHOLD can change, then it is not, by definition, a constant.
It’s just a global variable.

I’d change it to a method and then stub that method for a given
scenario (even though stubbing is a Cucumber no-no :wink: ).

WDYT?

David


#4

Good question. One answer is that it can change, it just needs a code
change
and application restart.

Basic business case is that someone will set the postage threshold
before
the application is deployed. However they could choose any value. So I
want
to test what happens when the application is setup with different values
e.g. zero because all postage is free and £30 cos postage is free if you
spend £50 or more.

Currently we do not want to do form based admin for this setting (and
similar ones) because such a change would be very infrequent, so we are
currently happy to redeploy to make the change.

2009/4/27 aslak hellesoy removed_email_address@domain.invalid


#5

similar ones) because such a change would be very infrequent, so we are
currently happy to redeploy to make the change.

I would do something like this: http://gist.github.com/102586


#6

I guess I’ll just have to use a global, perhaps trying to freeze them in
production. Just thought there might be something that might be an
alternative. Might have a look at using a stub if that doesn’t work.
Thanks
for your input

2009/4/27 David C. removed_email_address@domain.invalid


#7

Thankyou Aslak much appreciated

Andrew

2009/4/27 aslak hellesoy removed_email_address@domain.invalid


#8

On 27 Apr 2009, at 19:08, Andrew P. wrote:

Thankyou Aslak much appreciated

Bear in mind that this trick will change the value of the constant FOR ALL
REMAINING SCENARIOS - not just the current one.

I think you missed the After block.


#9

On 27 Apr 2009, at 19:08, Andrew P. wrote:

Thankyou Aslak much appreciated

Bear in mind that this trick will change the value of the constant FOR
ALL REMAINING SCENARIOS - not just the current one.

If that bothers you, have a read of this thread:
http://www.nabble.com/AfterCurrentScenario-block-td23100686.html

before the application is deployed. However they could choose any

module MVOR
implement
complex solutions for what I assume is something very simple.



rspec-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/rspec-users

Matt W.
http://blog.mattwynne.net
http://www.songkick.com


#10

2009/4/28 Andrew P. removed_email_address@domain.invalid

I guess I’ll just have to use a global, perhaps trying to freeze them in
production. Just thought there might be something that might be an
alternative. Might have a look at using a stub if that doesn’t work. Thanks
for your input

You could pass the value as a constructor parameter into objects that
use
it, defaulted to the constant:

def initialize(threshold = THRESHOLD)
@threshold = threshold

end

That way the default constructor would Do The Right Thing and you could
pass
in values for testing. It is also more intention-revealing than
referencing
global constants. (Where do you use the threshold? What for?)

Cheers,
Dan


#11

On 27 Apr 2009, at 22:48, aslak hellesoy wrote:

On 27 Apr 2009, at 19:08, Andrew P. wrote:

Thankyou Aslak much appreciated

Bear in mind that this trick will change the value of the constant
FOR ALL REMAINING SCENARIOS - not just the current one.

I think you missed the After block.

I stand corrected :slight_smile:

Good question. One answer is that it can change, it just needs a
so we are currently happy to redeploy to make the change.
wrote:

why are you testing it with different values?
removed_email_address@domain.invalid
rspec-users mailing list
rspec-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/rspec-users

Matt W.
http://blog.mattwynne.net
http://www.songkick.com


#12

You could pass the value as a constructor parameter into objects that use
it, defaulted to the constant:

def initialize(threshold = THRESHOLD)
@threshold = threshold

end

I agree. This probably also means dropping down a level - from Cucumber
to
RSpec.

That way the default constructor would Do The Right Thing and you could
pass in values for testing. It is also more intention-revealing than
referencing global constants. (Where do you use the threshold? What for?)