TCR is a very lightweight version of VCR for TCP sockets.
Currently used for recording 'net/smtp' interactions so only a few of the TCPSocket methods are recorded out.
Add this line to your application's Gemfile:
gem 'tcr'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tcr
require 'test/unit'
require 'tcr'
TCR.configure do |c|
c.cassette_library_dir = 'fixtures/tcr_cassettes'
c.hook_tcp_ports = [2525]
end
class TCRTest < Test::Unit::TestCase
def test_example_dot_com
TCR.use_cassette('mandrill_smtp') do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
assert_match /220 smtp.mandrillapp.com ESMTP/, io.readline
end
end
end
Run this test once, and TCR will record the tcp interactions to fixtures/tcr_cassettes/google_smtp.json.
[
[
[
"read",
"220 smtp.mandrillapp.com ESMTP\r\n"
]
]
]
Run it again, and TCR will replay the interactions from json when the tcp request is made. This test is now fast (no real TCP requests are made anymore), deterministic and accurate.
You can disable TCR hooking TCPSocket ports for a given block via turned_off
:
TCR.turned_off do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request