[rspec-users] Augmenting a previous ''let'' value

J. B. Rainsberger
Fri, 21 Dec 2012 19:10:15 -0000

If you declare :hsh in two peer context (neither inside the other), then
your shared group does the right thing: it demands the existence of :hsh
and blows up when it doesn''t exist. Better design, no?



On Fri, Dec 21, 2012 at 9:24 AM, Arne Brasseur <arne.brasseur at
gmail.com>wrote:

> I find myself wanting to do something like this, and I''m curious
to
> hear what others think of this pattern.
>
> describe "a hash with two values" do
>   let(:hsh) {{ xxx: 1, yyy: 2 }}
>
>   context "with a third value added" do
>     alias _hsh hsh
>     let(:hsh) { _hsh.merge(zzz: 3) }
>
>     it "should contain all three values" do
>       hsh.should == { xxx:1, yyy:2, zzz:3 }
>     end
>   end
> end
>
> The alias there is a workaround, ideally I''d like hsh to reference
the
> previous value when inside that block : let(:hsh) { hsh.merge(zzz: 3)
> } , but with the current implementation that gives infinite recursion.
>
> Another way is to use a before block, but I don''t like the mixing
of
> let and before in this case.
>
> describe "a hash with two values" do
>   let(:hsh) {{ xxx: 1, yyy: 2 }}
>
>   context "with a third value added" do
>     before { hsh.merge!(zzz: 3) }
>
>     it "should contain all three values" do
>       hsh.should == { xxx:1, yyy:2, zzz:3 }
>     end
>   end
> end
>
> I don''t want to use a different name, because I have shared
examples
> that reference it. I know there are ways to side step the issue.
>
> Have you used something like this before? Would you consider it bad
> practice?
>
> - Arne
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
J. B. (Joe) Rainsberger :: http://www.myagiletutor.com ::
http://www.jbrains.ca ::
http://blog.thecodewhisperer.com
Free Your Mind to Do Great Work :: http://www.freeyourmind-dogreatwork.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20121221/11e97d29/attachment.html>