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.
85d99e7678d8720f6e00ab0f60fe6ea9?d=identicon&s=25 Andrew Premdas (Guest)
on 2009-04-27 16: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
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-04-27 16:03
(Received via mailing list)
On Mon, Apr 27, 2009 at 2:00 PM, Andrew Premdas <apremdas@gmail.com>
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?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-04-27 16:38
(Received via mailing list)
On Mon, Apr 27, 2009 at 7:00 AM, Andrew Premdas <apremdas@gmail.com>
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
85d99e7678d8720f6e00ab0f60fe6ea9?d=identicon&s=25 Andrew Premdas (Guest)
on 2009-04-27 18: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 <aslak.hellesoy@gmail.com>
85d99e7678d8720f6e00ab0f60fe6ea9?d=identicon&s=25 Andrew Premdas (Guest)
on 2009-04-27 18: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 Chelimsky <dchelimsky@gmail.com>
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-04-27 18: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
85d99e7678d8720f6e00ab0f60fe6ea9?d=identicon&s=25 Andrew Premdas (Guest)
on 2009-04-27 20:15
(Received via mailing list)
Thankyou Aslak much appreciated

Andrew

2009/4/27 aslak hellesoy <aslak.hellesoy@gmail.com>
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-04-27 23:03
(Received via mailing list)
On 27 Apr 2009, at 19:08, Andrew Premdas 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
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-04-28 00:20
(Received via mailing list)
>
> On 27 Apr 2009, at 19:08, Andrew Premdas 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.
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-04-28 02:34
(Received via mailing list)
On 27 Apr 2009, at 22:48, aslak hellesoy wrote:

>
>
> On 27 Apr 2009, at 19:08, Andrew Premdas 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?
> rspec-users@rubyforge.org
> rspec-users mailing list
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
Fd8f09626613a93a79e2ae899f00a465?d=identicon&s=25 Dan North (Guest)
on 2009-04-28 08:16
(Received via mailing list)
2009/4/28 Andrew Premdas <apremdas@gmail.com>

> 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
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-04-28 08: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.