# [cucumber] Setting a constant in step definition

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

On Mon, Apr 27, 2009 at 2:00 PM, Andrew P. [email protected]
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?

On Mon, Apr 27, 2009 at 7:00 AM, Andrew P. [email protected]
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 ).

WDYT?

David

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 [email protected]

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: hooks.rb · GitHub

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

2009/4/27 David C. [email protected]

Thankyou Aslak much appreciated

Andrew

2009/4/27 aslak hellesoy [email protected]

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.

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
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Matt W.
http://blog.mattwynne.net

2009/4/28 Andrew P. [email protected]

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

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

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

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?
[email protected]
rspec-users mailing list
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Matt W.
http://blog.mattwynne.net

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?)