[rspec-users] passing a block to a stub method

755 weergaven
Naar het eerste ongelezen bericht

nicolas

ongelezen,
13 sep 2010, 18:32:3113-09-2010
aan rspec...@rubyforge.org
It seems that rspec doesn't support passing a block to a stub method.
This code fails:

def call_build_with_block(o)
o.build do
puts "hello"
end
end


describe "call_build_with_block" do
it "should call build with block" do
o = double("object")
o.should_receive(:build) do |&b|
b.should_not be_nil
block_given?.should == true
end

call_build_with_block(o)
end
end

Anybody?
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

ongelezen,
13 sep 2010, 22:31:5613-09-2010
aan rspec-users
On Sep 13, 2010, at 5:32 PM, nicolas wrote:

> It seems that rspec doesn't support passing a block to a stub method.
> This code fails:
>
> def call_build_with_block(o)
> o.build do
> puts "hello"
> end
> end
>
>
> describe "call_build_with_block" do
> it "should call build with block" do
> o = double("object")
> o.should_receive(:build) do |&b|
> b.should_not be_nil
> block_given?.should == true
> end
>
> call_build_with_block(o)
> end
> end
>
> Anybody?

Bug. Please file a bug report at http://github.com/rspec/rspec-mocks/issues.

Thx,
David

Justin Ko

ongelezen,
13 sep 2010, 22:38:0213-09-2010
aan rspec...@rubyforge.org

On Sep 13, 6:32 pm, nicolas <nicolas.br...@gmail.com> wrote:
> It seems that rspec doesn't support passing a block to a stub method.
> This code fails:
>
> def call_build_with_block(o)
>   o.build do
>     puts "hello"
>   end
> end
>
> describe "call_build_with_block" do
>   it "should call build with block" do
>     o = double("object")
>     o.should_receive(:build) do |&b|
>       b.should_not be_nil
>       block_given?.should == true
>     end
>
>     call_build_with_block(o)
>   end
> end
>
> Anybody?
> _______________________________________________
> rspec-users mailing list
> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

What you can use is the "and_yield" method:

o.should_receive(:build).and_yield do |eval_context|
eval_context.should_not be_nil
end

Hope that helps.

Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten