Hopefully a simple question for MiniTest folks..
I have a section of code which I'll condense into an example here:
class Foo
def initialize(name)
@sqs = Aws::SQS::Client.new
@id = @sqs.create_queue( queue_name: name ).fetch(:queue_url)
@poller = Aws::SQS::QueuePoller.new(@id)
end
def pick_first
@poller.poll(idle_timeout: 60) do |message|
process_msg(message) if some_condition(message)
end
end
How can I mock/stub/something-else so that I can feed a message
through to be tested by the some_condition()
and possibly processed with process_msg()
?
I.e. I want to test the @poller.poll(idle_timeout: 60) do |message|
.
I have tried to stub the Aws::SQS::QueuePoller#new
with a mock poller, but it's not yielding the message to |message|
just returning it..
This is what I have, which is not working:
mockqueue = MiniTest::Mock.new
mocksqs = MiniTest::Mock.new
mocksqs.expect :create_queue, mockqueue, [Hash]
mockpoller = MiniTest::Mock.new
mockpoller.expect :poll, 'message', [{ idle_timeout: 60 }]
Aws::SQS::Client.stub :new, mocksqs do
Aws::SQS::QueuePoller.stub :new, mockpoller do
queue = Foo.new(opts)
queue.pick_first
end
end
If I receive a variable in #pick_first
, that's where the mock puts it, not into |message|
:
def pick_first
receiver = @poller.poll(idle_timeout: 60) do |message|
process_msg(message) if some_condition(message)
end
puts receiver # this shows my 'message' !!! WHYYYY??
end
Answering my own question, in case someone else has the same question.
I asked for help on this via Twitter, and the author of MiniTest, Ryan Davis (aka @zenspider on github / @the_zenspider on Twitter) gave a quick answer along with an invite to submit the question to the MiniTest github issue tracker.
I did so, and got a couple of great responses, from Ryan and also from Pete Higgins (@phiggins on github), which I reproduce here in their entirety. Thank you to both of you for your help!
@phiggins said:
@zenspider said: