The problem with block arguments is there priority with Ruby… Consider that
this:
it “returns my value" do
expect(Foo).to receive(:baz) { “my return value” }
end
Needs to return the same as this:
it “still returns my value" do
expect(Foo).to receive(:baz).with(:my_argument) { “my return value” }
end
And that is the same as this:
it “still returns my value” do
my_block = lambda { “my return value” }
expect(Foo).to receive(:baz).with(:my_argument,&my_block)
end
And hopefully you can see why it doesn’t (and intend can’t) work the way you
want it currently…
I’d be open to adding another way to match this however, maybe an argument
matcher?
it "passes the given block to Foo" do
my_block = lambda {}
# pseudo code, won’t work atm
expect(Foo).to receive(:baz).with(block_argument(my_block))
Bar.baz_wrapper(&my_block)
end
Jon Rowe
---------------------------
[email protected]
jonrowe.co.uk
On Friday, 19 February 2016 at 09:47, Nathan Wenneker wrote:
> I want to verify that this method passes a block to a collaborator:
>
>
> module Bar
> def self.baz_wrapper(&block)
> Foo.baz(&block)
> end
> end
>
> Something like this would be nice:
>
> it "passes the given block to Foo" do
> my_block = lambda {}
>
> expect(Foo).to receive(:baz).with(&my_block)
>
> Bar.baz_wrapper(&my_block)
> end
> However, the best I could come up with is this:
>
>
> it "passes the given block to Foo" do
> my_block = lambda {}
>
> expect(Foo).to receive(:baz) do |&block|
> expect(block).to eq(my_block)
> end
>
> Bar.baz_wrapper(&my_block)
> end
> Questions.
>
> 1. Is there a better way to do this?
>
> 2. If not, would you be open to a discussion on making `expect(Foo).to
> receive(:baz).with(&my_block)` work?
>
>
> Thank you,
>
> Nathan
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> (mailto:[email protected]).
> To post to this group, send email to [email protected]
> (mailto:[email protected]).
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/3bfa0a8a-4f55-4e24-8527-3f2f4eb693ea%40googlegroups.com
>
> (https://groups.google.com/d/msgid/rspec/3bfa0a8a-4f55-4e24-8527-3f2f4eb693ea%40googlegroups.com?utm_medium=email&utm_source=footer).
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/EB121FFFE7C64891924A7F6D1148B377%40jonrowe.co.uk.
For more options, visit https://groups.google.com/d/optout.