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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081015/726c33bb/attachment.html>
On Wed, Oct 15, 2008 at 4:39 PM, Craig Demyanovich <cdemyanovich at gmail.com> wrote:> Probably, I would just check the outcome of the method instead of checking > interaction with a constant. >What he said, -- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
On 2008-10-15, at 16:39, Craig Demyanovich wrote:> Probably, I would just check the outcome of the method instead of > checking interaction with a constant. > > CraigSo 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 Hoffman <nick at deadorange.com> wrote:> On 2008-10-15, at 16:39, Craig Demyanovich 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081015/62f42a29/attachment.html>
On Oct 15, 2008, at 4:31 PM, Nick Hoffman wrote:> 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?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
On 2008-10-15, at 21:59, Craig Demyanovich 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, > CraigHi 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081016/230a45b9/attachment.html>
On 2008-10-16, at 15:12, Craig Demyanovich 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, > CraigThanks for taking a look, Craig, and giving me your opinion. Cheers, Nick
Marcelo de Moraes Serpa
2010-Feb-18 17:58 UTC
[rspec-users] How to spec accessing a constant
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 Thu, Oct 16, 2008 at 7:24 PM, Nick Hoffman <nick at deadorange.com> wrote:> On 2008-10-16, at 15:12, Craig Demyanovich 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 > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100218/38850feb/attachment.html>