Skip to content

LTe/object-history

Repository files navigation

object-history

Test your objects with history path. Simply select the method around which will create a deep copy of an object and see how the object changed during operation.

Instalation

Add to your Gemfile

gem 'object-history'

How to use

Just include ObjectHistory to class.

class TrackedObject
  include ObjectHistory
end

And on bottom of class add the methods that you want to track

class TrackedObject
  include ObjectHistory
  track_history_of :your_method
end

Examples

Track object

class TrackedObject
  attr_accessor :number

  def initialize(child = nil)
    @number = 0
  end

  def add_one(*args, &block)
    @number += 1
    block.call(self) if block_given?
  end

  include ObjectHistory
  track_history_of :add_one
end

After that you can test with rspec using have_track matcher ;-)

describe TrackObject do
  before(:each) do
    @track_object = TrackObject.new
  end

  it "should every execute add one to :number" do
    3.times {@track_object.add_one}
    @track_object.should have_track(:number, [0,1,2,3])
  end
end

Post example

class Post
  attr_accessor :status

  def initialize
    @status = :new
  end

  def accept
    @status = :accepted
  end

  def send_post
    @status = :sent
  end

  def load
    @status = :loaded
  end

  include ObjectHistory
  track_history_of :accept, :send_post, :load
end

And Rspec

before(:each) do
  @post = Post.new
end

it "should have [:new, :loaded, :accepted, :sent] path" do
  @post.load
  @post.accept
  @post.send_post

  @post.should have_track(:status, [:new, :loaded, :accepted, :sent])
end

Accessors

class Accessor
  attr_accessor :version

  def initialize
    @version = 0
  end

  include ObjectHistory 
  track_history_of :version=
end

Rspec

before(:each) do
  @accessor = Accessor.new
end

it "should track accessor" do
  @accessor.version = 5
  @accessor.version = 10

  @accessor.should have_track(:version, [0,5,10])
end

Deep clone?

Works

method_missing?

Fuck method_missing ;*. Just deal with it.

Contributing to object-history

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Piotr Niełacny. See LICENSE.txt for further details.

About

Track your object with rspec matcher

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages