When setting expectations on an existing stub, block to evaluate responses is not executed #224

Closed
wants to merge 1 commit into from

2 participants

@mkilling

Hi,

this works (block is executed):

http_request(:get, "http://www.example.com/")
assert_requested(:get, "http://www.example.com/", :times => 1) { |req|
    # evaluate req
}

but this does not (block is not executed):

stub = stub_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
assert_requested(stub, :times => 1) { |req|
   # evaluate req
}

I would expect the second example to work just like the first one.

@mkilling mkilling Test expectation block added to a stub
Current behavior: When setting expectations on a stub variable that
include a block, the block is not executed when the expectations are
being evaluated.

Expected behavior: I would want the block to be evaluated just like it
is evaluated when not setting the expectation on a stub existing
before.
ca81ef1
@bblimke
Owner

@mkilling What would be the meaning of a block provided in case stub object is provided as an argument too?

The block provided to assert_requested is an assertion block, which is then attached to a dynamically created stub.

In case a stub is provided as an argument then it is expected that the sub object will already have all assertions included.

You can declare a stub with a block like that stub_request(:get, "http://www.example.com/").with { called = true }

@mkilling

If this is by design I'm ok with it.
What I'm trying to do is a bit convoluted. I have a stub and I want to extract the request body sent to it in just one test case.

request_body = nil
assert_requested(@notification_stub, :times => 1) { |req|
  request_body = req.body
  true
}
# assertions on request_body

I expected this to just work, which is why I filed the issue.
The workaround I went with is to use just the url of instead of the whole stub:

request_body = nil
assert_requested(:post, @notification_url, :times => 1) { |req|
  request_body = req.body
  true
}
# assertions on request body

You could also make @request_body an instance variable and write to it every time the stub is called.

@bblimke
Owner

Yes, I guess I should raise an error is both stub and block are provided to assert_requested

Why don't you do the following:

request_body = nil
stub_request(....).with{ |req|
  request_body = req.body
  true
}
assert_requested(@notification_stub, :times => 1) 
# assertions on request_body
@mkilling

I'll look into it, thanks!

@bblimke bblimke closed this Feb 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment