How to spec accessing a constant

Hi guys. One of my methods uses a constant in another method, like this:

class A
def something
“foo: #{B::BAR}”
end
end

When writing the spec for A#something , how would you mock or stub
#{B::BAR}, and how would you set an expectation that B::BAR is used?

Thanks,
Nick

Probably, I would just check the outcome of the method instead of
checking
interaction with a constant.

Craig

On Wed, Oct 15, 2008 at 4:39 PM, Craig D.
[email protected] wrote:

Probably, I would just check the outcome of the method instead of checking
interaction with a constant.

What he said,


Zach D.
http://www.continuousthinking.com

On 2008-10-15, at 16:39, Craig D. wrote:

Probably, I would just check the outcome of the method instead of
checking interaction with a constant.

Craig

So you guys wouldn’t worry about the spec for class A being coupled to
this constant in class B?
-Nick

On Wed, Oct 15, 2008 at 8:47 PM, Nick H. [email protected]
wrote:

On 2008-10-15, at 16:39, Craig D. wrote:

Probably, I would just check the outcome of the method instead of checking
interaction with a constant.

Craig

So you guys wouldn’t worry about the spec for class A being coupled to this
constant in class B?

Since class A is coupled to class B, the specs for A are also coupled to
class B through class A. Thus, I wouldn’t worry about the coupling. Why
does
a method of class A directly access a constant of class B? Does the
constant
belong in class A? Does the method belong in class B? If you can and
want to
be more specific with your code and specs, I’m sure that we can all
write
some specs together.

Regards,
Craig

On 2008-10-15, at 21:59, Craig D. wrote:

Since class A is coupled to class B, the specs for A are also
coupled to class B through class A. Thus, I wouldn’t worry about the
coupling. Why does a method of class A directly access a constant of
class B? Does the constant belong in class A? Does the method belong
in class B? If you can and want to be more specific with your code
and specs, I’m sure that we can all write some specs together.

Regards,
Craig

Hi Craig. Here’re some code snippets:
http://pastie.org/293925

Property#javascript_map_marker_code generates the Javascript code
necessary to:

  1. Create a [Google] map marker that represents a property instance.
  2. Add the marker to the map.
    To perform #2, RentalMap::MAP_NAME must be accessed somehow, be it
    directly, or through a method as Scott suggested.

RentalMap::MAP_NAME should definitely be part of the RentalMap model.
It should not be part of the Property model.

Property#javascript_map_marker_code belongs in the Property model,
because it acts upon (IE: uses several attributes of) a Property
instance.

Cheers,
Nick

Cool. Having seen something a little more concrete, I like your design
decisions. In this case, I’d go with Scott’s recommendation of hiding
the
constant behind a method.

Regards,
Craig

On 2008-10-16, at 15:12, Craig D. wrote:

Cool. Having seen something a little more concrete, I like your
design decisions. In this case, I’d go with Scott’s recommendation
of hiding the constant behind a method.

Regards,
Craig

Thanks for taking a look, Craig, and giving me your opinion.

Cheers,
Nick

Why don’t you open the class, and set the constant like so:

class TheClass
CONSTANT = ‘value_it_should_have_for_the_current_spec’
end

This worked for me.

Marcelo.

On Oct 15, 2008, at 4:31 PM, Nick H. wrote:

#{B::BAR}, and how would you set an expectation that B::BAR is used?
Just hide the constant behind a method, something like this:

class A
def something; “foo :#{bar}”; end
def bar; B::BAR; end
end

This allows you to stub the constant, if need be.

Scott