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

2012-12-21 Thread Arne Brasseur
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@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


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

2012-12-21 Thread J. B. Rainsberger
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 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@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
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users