Code Monkey home page Code Monkey logo

delorean's Introduction

Delorean Build Status

Delorean lets you travel in time with Ruby by mocking Time.now

Marty:: Wait a minute, Doc. Ah... Are you telling me that you built a time machine... out of a DeLorean?

Doc:: The way I see it, if you're gonna build a time machine into a car, why not do it with some style?

Install

$ [sudo] gem install delorean

Or add it to your Gemfile, etc.

Usage

Let's travel in time!

require 'delorean'

# Date.today => Wed Feb 24
Delorean.time_travel_to "1 month ago" # Date.today => Sun Jan 24
Delorean.back_to_the_present          # Date.today => Wed Feb 24

With a block:

Delorean.time_travel_to("1 month ago") do
  # Inside the block, Time.now => Sun Jan 24 00:34:32 +0100 2010
  sleep(5)
  # And the time still goes by... Time.now => Sun Jan 24 00:34:37 +0100 2010
end

# Outside the block, Time.now => Wed Feb 24 00:34:35 +0100 2010

You can also jump which is like sleep but without losing time

# Time.now => Wed Feb 24 00:34:04 +0100 2010
Delorean.jump 30
# Time.now => Wed Feb 24 00:34:34 +0100 2010

Testing

Time-travelling can be extremely useful when you're testing your application.

For example, in RSpec you may find convenient to include Delorean's DSL in your spec_helper.rb:

RSpec.configure do |config|
  config.include Delorean
  ...

Now you can time-travel in your examples, like this:

it "should show latest created user" do

  time_travel_to(3.minutes.ago) { create_user :name => "John"  }
  time_travel_to(5.minutes.ago) { create_user :name => "Chris" }

  get 'show'

  response.should have_text("John")
  response.should_not have_text("Chris")

end

Don't forget to go back to the present after each example:

after(:each) { back_to_the_present }

or its alternate syntax:

after(:each) { back_to_1985 }

Credits

Delorean image based on an original by Giancarlo Pitocco.

Copyright (c) 2010-2011 Luismi Cavallé, Sergio Gil and BeBanjo S.L. released under the MIT license

delorean's People

Contributors

calavera avatar cavalle avatar nbibler avatar porras avatar retr0h avatar russellporter avatar tomykaira avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.