Forum: RSpec [cucumber] Setting a constant in step definition

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrew P. (Guest)
on 2009-04-27 18:03
(Received via mailing list)
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
Aslak H. (Guest)
on 2009-04-27 18:03
(Received via mailing list)
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?
David C. (Guest)
on 2009-04-27 18:38
(Received via mailing list)
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 ;) ).

WDYT?

David
Andrew P. (Guest)
on 2009-04-27 20:24
(Received via mailing list)
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>
Andrew P. (Guest)
on 2009-04-27 20:32
(Received via mailing list)
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>
Aslak H. (Guest)
on 2009-04-27 20:37
(Received via mailing list)
> 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
Andrew P. (Guest)
on 2009-04-27 22:15
(Received via mailing list)
Thankyou Aslak much appreciated

Andrew

2009/4/27 aslak hellesoy <removed_email_address@domain.invalid>
Matt W. (Guest)
on 2009-04-28 01:03
(Received via mailing list)
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-t...

> 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
Aslak H. (Guest)
on 2009-04-28 02:20
(Received via mailing list)
>
> 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.
Matt W. (Guest)
on 2009-04-28 04:34
(Received via mailing list)
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?
> 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
Dan N. (Guest)
on 2009-04-28 10:16
(Received via mailing list)
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
Aslak H. (Guest)
on 2009-04-28 10:24
(Received via mailing list)
> 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?)
>
This topic is locked and can not be replied to.