Forum: RSpec How to spec accessing a constant

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.
Nick H. (Guest)
on 2008-10-16 00:32
(Received via mailing list)
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
Craig D. (Guest)
on 2008-10-16 00:39
(Received via mailing list)
Probably, I would just check the outcome of the method instead of
checking
interaction with a constant.

Craig
Zach D. (Guest)
on 2008-10-16 00:43
(Received via mailing list)
On Wed, Oct 15, 2008 at 4:39 PM, Craig D.
<removed_email_address@domain.invalid> 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
http://www.mutuallyhuman.com
Nick H. (Guest)
on 2008-10-16 04:52
(Received via mailing list)
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
Craig D. (Guest)
on 2008-10-16 05:59
(Received via mailing list)
On Wed, Oct 15, 2008 at 8:47 PM, Nick H. <removed_email_address@domain.invalid>
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
Scott T. (Guest)
on 2008-10-16 09:48
(Received via mailing list)
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
Nick H. (Guest)
on 2008-10-16 19:23
(Received via mailing list)
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
Craig D. (Guest)
on 2008-10-16 23:13
(Received via mailing list)
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
Nick H. (Guest)
on 2008-10-17 01:25
(Received via mailing list)
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
Marcelo de Moraes S. (Guest)
on 2010-02-18 20:26
(Received via mailing list)
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.
This topic is locked and can not be replied to.