Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It should be possible to set an expectation that a method is called with a block #21

Open
jackdempsey opened this issue May 18, 2013 · 2 comments
Labels

Comments

@jackdempsey
Copy link

Perhaps get at the block somehow and verify things about it (what happens when you call it, etc).

I find some some code that passes a block to a method, and inside the block is a string. I'd like to verify that with a spec before I start changing it.

@jackdempsey
Copy link
Author

I seem to have gotten around what I believe is a limitation with mocks and block passing by doing something like this:

mock(Klass).run.with_any_args do |*args|
block_that_was_passed = args.last
block_that_was_passed.should eq some_verifcation_string
end

If there's a more idiomatic way, love to see it, otherwise I guess this is closed.

@mcmire
Copy link
Contributor

mcmire commented May 18, 2013

No, there isn't a built-in way to do this. I don't know if I plan on adding support for this any time soon, but it's an interesting use case. The workaround you came up with looks like the best solution so far (although you need to say block.call and not simply block).

It's also curious to me why it doesn't work like this instead:

mock(Klass).run.with_any_args do |*args, &block|
  block.should eq some_verification_string
end

I'll go ahead and keep this issue open because I want to look into that in particular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants