Skip to content
Olia Kremmyda edited this page Mar 21, 2018 · 1 revision

Test::Unit

is an xUnit family unit testing framework for Ruby.

More info on the framework can be found here http://test-unit.github.io/

Usage

Add the following code to test/test_helper.rb

require 'webmock/test_unit'

Setting expectations

require 'webmock/test_unit'

stub_request(:any, "www.example.com")

uri = URI.parse('http://www.example.com/')
req = Net::HTTP::Post.new(uri.path)
req['Content-Length'] = 3

res = Net::HTTP.start(uri.host, uri.port) do |http|
  http.request(req, 'abc')
end

assert_requested :post, "http://www.example.com",
  headers: {'Content-Length' => 3}, body: "abc",
  times: 1    # ===> Success

assert_not_requested :get, "http://www.something.com"    # ===> Success

assert_requested(:post, "http://www.example.com",
  times: 1) { |req| req.body == "abc" }

Expecting real (not stubbed) requests

WebMock.allow_net_connect!

Net::HTTP.get('www.example.com', '/')    # ===> Success

assert_requested(:get, "http://www.example.com")    # ===> Success

Setting expectations on the stub

stub_get = stub_request(:get, "www.example.com")
stub_post = stub_request(:post, "www.example.com")

Net::HTTP.get('www.example.com', '/')

assert_requested(stub_get)
assert_not_requested(stub_post)