Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Store Multiple Requests for Inspection #27

Open
wants to merge 2 commits into from

5 participants

@paulbellamy

What I did:

  • Refactored @last_request into an array called @requests
  • FakeWeb.last_request=(request) now appends to @requests
  • FakeWeb.last_request returns @requests.last (maintains backwards compatibility)
  • FakeWeb.clear_requests sets @requests to nil (clearing received requests)
  • FakeWeb.requests returns @requests for inspection

Use Case: When code makes multiple requests and you care about a request other than the last one made.

Usage: - To check the last request made by a test I do FakeWeb.last_request (backwards compatible)

  • To check the first request for a test I do FakeWeb.requests.first
  • To check the second-last request I do FakeWeb.requests[-2]
  • Etc...
  • After each test (where I was doing FakeWeb.clean_registry), I add a call to FakeWeb.clear_requests
paulbellamy added some commits
@paulbellamy paulbellamy Added .rvmrc to gitignore 62063fb
@paulbellamy paulbellamy Adding multiple request storage
  * Allows you to inspect more than just the last request
  * Fully backward compatible
  * Provides FakeWeb.clear_requests which clears the log of received requests
51e47fa
@openfirmware

Interesting we both came up with similar solutions: https://github.com/openfirmware/fakeweb/tree/multiple_recent_requests

I like your tests better than mine, though.

@gregory-m

+1 for this one.
I also sometimes need this functionality in my specs.

@jshou

+1!

@pat

Excuse me pimping something of my own, but I wrote a gem some time ago that lets you set expectations on which requests have been made: https://github.com/freelancing-god/fakeweb-matcher

Granted, it's built for RSpec, but could likely also be used in TestUnit.

@jshou

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 5, 2011
  1. @paulbellamy

    Added .rvmrc to gitignore

    paulbellamy authored
  2. @paulbellamy

    Adding multiple request storage

    paulbellamy authored
      * Allows you to inspect more than just the last request
      * Fully backward compatible
      * Provides FakeWeb.clear_requests which clears the log of received requests
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -7,3 +7,4 @@
/.idea
*.rbc
Gemfile.lock
+.rvmrc
View
13 lib/fake_web.rb
@@ -194,11 +194,20 @@ def self.registered_uri?(*args)
# Returns the request object from the last request made via Net::HTTP.
def self.last_request
- @last_request
+ requests.last
end
def self.last_request=(request) #:nodoc:
- @last_request = request
+ @requests ||= []
+ @requests << request
+ end
+
+ def self.requests
+ @requests ||= []
+ end
+
+ def self.clear_requests
+ @requests = []
end
private
View
1  test/test_helper.rb
@@ -15,6 +15,7 @@ class Test::Unit::TestCase
alias setup_without_fakeweb setup
def setup
FakeWeb.clean_registry
+ FakeWeb.clear_requests
@original_allow_net_connect = FakeWeb.allow_net_connect?
FakeWeb.allow_net_connect = false
end
View
36 test/test_last_request.rb
@@ -26,4 +26,40 @@ def test_last_request_has_correct_method_path_and_body_for_post
assert_equal 10, FakeWeb.last_request.content_length
end
+ def test_requests_returns_array_of_latest_requests
+ FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
+ FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
+ Net::HTTP.start("example.com") { |http| http.get("/") }
+ Net::HTTP.start("example.com") { |http| http.post("/posts", "title=Test") }
+ requests = FakeWeb.requests
+ assert_equal "GET", requests[0].method
+ assert_equal "/", requests[0].path
+ assert_nil requests[0].body
+ assert_nil requests[0].content_length
+ assert_equal "POST", requests[1].method
+ assert_equal "/posts", requests[1].path
+ assert_equal "title=Test", requests[1].body
+ assert_equal 10, requests[1].content_length
+ end
+
+ def test_last_request_returns_latest_request
+ FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
+ FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
+ Net::HTTP.start("example.com") { |http| http.get("/") }
+ Net::HTTP.start("example.com") { |http| http.post("/posts", "title=Test") }
+ assert_equal FakeWeb.last_request, FakeWeb.requests.last
+ end
+
+ def test_clear_requests_clears_requests
+ FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
+ FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
+ Net::HTTP.start("example.com") { |http| http.get("/") }
+ Net::HTTP.start("example.com") { |http| http.post("/posts", "title=Test") }
+ assert_equal FakeWeb.requests.size, 2
+ assert_not_nil FakeWeb.last_request
+ FakeWeb.clear_requests
+ assert_equal FakeWeb.requests.size, 0
+ assert_nil FakeWeb.last_request
+ end
+
end
Something went wrong with that request. Please try again.