GithubHelp home page GithubHelp logo

netrecorder's Introduction

NetRecorder

Record network responses for easy stubbing of external calls.

Net recorder hooks into NetHTTP to record the outgoing request path and method and caches the response. Then you can switch on fakeweb and it will use the cache from the recording.

Requirements

gem install fakeweb

Install

gem install netrecorder

Usage

Anywhere you use fakeweb, you can use net recorder.

Record all responses:

NetRecorder.config do |config|
  config.cache_file       = File.join(RAILS_ROOT, 'fakeweb')
  config.record_net_calls = true
end

Save recorded responses:

NetRecorder.cache!

Use recorded cache with fakeweb:

NetRecorder.config do |config|
  config.cache_file = File.join(RAILS_ROOT, 'features', 'support', 'fakeweb')
  config.fakeweb    = true
end

Scopes

Net recorder makes it easy to add a ‘scope’ to your recordings. Fakeweb doesn’t have a way to name a response. If you make a request to the same url twice and get a different response both times, fakeweb just stores the responses in an array and returns them in order. This means that whatever you are testing will have to maintain the order or else you won’t get the correct mocked data for your tests. So netrecorder allows you to set a scope that can be used when registering with fakeweb.

To record using scopes:

if NetRecorder.recording?
  NetRecorder.scope = 'first request'
  # request www.something.com
  # make a change that alters the return value from www.something.com
  NetRecorder.scope = "second request"
  # request www.something.com
end

To register the scoped cache with fakeweb:

if !NetRecorder.recording?
  FakeWeb.clean_registry # This will clear out any previous registrations so that our scope will not be added to the array of reponses
  NetRecorder.register_scope(scenario.name)
end

Cucumber Example

see cukes.info for more info on testing with Cucumber

# Find me in features/support/netrecorder.rb

NetRecorder.config do |config|
  config.cache_file = "#{File.dirname(__FILE__)}/../support/fakeweb"    
  if ENV['RECORD_WEB']
    config.record_net_calls = true
  else
    config.fakeweb = true
    FakeWeb.allow_net_connect = false
  end
end 

at_exit do
  if NetRecorder.recording?
    NetRecorder.cache!
  end
end

# Scope all the recordings to the scenario name!
Before do |scenario|
  if NetRecorder.recording?
    NetRecorder.scope = scenario.name
  else
    FakeWeb.clean_registry
    NetRecorder.register_scope(scenario.name)
  end
end

record mode (command line)

>> rake features RECORD_WEB=true

cache mode (command line)

>> rake features

Similar projects

StaleFish[http://github.com/jsmestad/stale_fish]

* Uses fixtures to store netrequests
* More configuration options
* Configure fixtures to atomically refresh themselves at an interval

netrecorder's People

Contributors

myronmarston avatar priteau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

netrecorder's Issues

fake random requests

Hi, netrecorder is great but it's good only for static contents.
I came across this scenario where I've to fake random requests.

Get same tweets but faked as random twitter users

Let's say our twitter user is randomly generated.
Then we 'RECORD_WEB' our first request to get tweets of user '12345'.
Now, if i try using netrecorder in normal mode then the user_id(say 'abcd') of both request and response won't be matched as its generated randomly.

It would be nice to get same result in normal mode while only the user specific parameters being faked.
Is there any other solution for this?

uninitialized class variable @@fakes in Net::HTTP (NameError)

It works in ruby 1.8.7 but fails with ruby 1.9.2

/Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/netrecorder-0.2.3/lib/http.rb:39:in `fakes': uninitialized class variable @@fakes in Net::HTTP (NameError)
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/netrecorder-0.2.3/lib/netrecorder.rb:38:in `block in cache!'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/netrecorder-0.2.3/lib/netrecorder.rb:38:in `open'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/netrecorder-0.2.3/lib/netrecorder.rb:38:in `cache!'
    from /Users/millisami/apps/newsly/spec/spec_helper.rb:44:in `block (2 levels) in <top (required)>'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:22:in `call'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:22:in `call'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:41:in `run_in'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:70:in `block in run_all'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:70:in `each'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:70:in `run_all'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/hooks.rb:106:in `run_hook'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/command_line.rb:29:in `block in run'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/reporter.rb:12:in `report'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/command_line.rb:24:in `run'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:46:in `run'
    from /Users/millisami/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:10:in `block in autorun'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.