Created
March 2, 2010 23:39
-
-
Save freshtonic/320111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I'm testing some code that translates HTTP query params from the front end | |
# into a query against a Solr via the Sunspot API. What I want to be able to | |
# do is unit test the query translation step *without* having to have Solr running. | |
# So I thought about mocking it out, but none of the mocking frameworks seem to | |
# be able to provide a way of mocking out something passed as a block. | |
# I have some HTTP query that my code 'executes' against Solr. It should be *exactly* equivalent | |
# to running the following query against Sunspot. | |
Sunspot.search(Scrape) do | |
with(:domain).equal_to("some.domain.name.com") | |
end | |
# What I want to be able to do is somehow mock 'self' within the block so that I can | |
# test that with(:domain).equal_to("some.domain.name.com") is executed. | |
# So far I haven't figured out how to do it with a mocking framework. | |
# I want to be able to say something like this (verbose for clarity, mock framework is RR) | |
mock(Sunspot).search(Scrape).is_called_with_block_that_executes do |_self| | |
_self.with(:domain).equal_to("some.domain.name.com") | |
end | |
# Any ideas? | |
# I realise I could hand code a mock Sunspot.search method, or perhaps | |
# mock the internal Sunspot DSL class but that feels dirty. |
I think I got the solution:
catcher = mock('Catcher', :binding => binding)
catcher.binding.should_receive(:keywords).with('bob')
SunspotSearchableModel.should_receive(:search).and_return(catcher.binding)
Hm. maybe not.
Hi there!
I blogged about it (my one and only post...) http://freshtonic.tumblr.com/
On 9 December 2011 03:08, mmzoo < ***@***.*** > wrote:
I think I got the solution:
catcher = mock('Catcher', :binding => binding)
catcher.binding.should_receive(:keywords).with('bob')
SunspotSearchableModel.should_receive(:search).and_return(catcher.binding)
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/320111
##
James
Hah! Thanks, I just were about to use
class SunspotSearchTestModel
def self.search(&block)
instance_eval(&block)
end
end
SunspotSearchTestModel.should_receive(:keywords).with('john')
But your solution is really nicer. Thanks a lot!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you come up with a solution for this?