Skip to content
This repository

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 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 February 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 11, 2012
Marvin Killing 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
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 15 additions and 0 deletions. Show diff stats Hide diff stats

  1. 15  test/shared_test.rb
15  test/shared_test.rb
@@ -73,4 +73,19 @@ def test_verification_that_non_expected_stub_didnt_occur
73 73
       assert_not_requested(@stub_http)
74 74
     end
75 75
   end
  76
+
  77
+  def test_that_block_is_executed_when_setting_expectations_on_the_stub
  78
+    called = false
  79
+    stub = stub_request(:get, "http://www.example.com/")
  80
+    http_request(:get, "http://www.example.com/")
  81
+    assert_requested(stub, :times => 1) { called = true }
  82
+    assert called, "block was not executed"
  83
+  end
  84
+
  85
+  def test_that_block_is_executed_when_setting_expectations
  86
+    called = false
  87
+    http_request(:get, "http://www.example.com/")
  88
+    assert_requested(:get, "http://www.example.com/", :times => 1) { called = true }
  89
+    assert called, "block was not executed"
  90
+  end
76 91
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.