Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Marvin Killing Bartosz Blimke
Marvin Killing

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.

Marvin Killing 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
Bartosz Blimke
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 }

Marvin Killing

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.

Bartosz Blimke
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
Marvin Killing

I'll look into it, thanks!

Bartosz Blimke bblimke closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 11, 2012
  1. Marvin Killing

    Test expectation block added to a stub

    mkilling authored
    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.
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 0 deletions.
  1. +15 −0 test/shared_test.rb
15 test/shared_test.rb
View
@@ -73,4 +73,19 @@ def test_verification_that_non_expected_stub_didnt_occur
assert_not_requested(@stub_http)
end
end
+
+ def test_that_block_is_executed_when_setting_expectations_on_the_stub
+ called = false
+ stub = stub_request(:get, "http://www.example.com/")
+ http_request(:get, "http://www.example.com/")
+ assert_requested(stub, :times => 1) { called = true }
+ assert called, "block was not executed"
+ end
+
+ def test_that_block_is_executed_when_setting_expectations
+ called = false
+ http_request(:get, "http://www.example.com/")
+ assert_requested(:get, "http://www.example.com/", :times => 1) { called = true }
+ assert called, "block was not executed"
+ end
end
Something went wrong with that request. Please try again.