Coder Social home page Coder Social logo

cutalion / no-peeping-toms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from patmaddox/no-peeping-toms

1.0 1.0 0.0 180 KB

Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer. Also lets you enable observers for a certain block of code, so that you can write observer tests

Home Page: http://www.patmaddox.com/blog/2007/11/22/better-rails-testing-decoupling-observers

Ruby 100.00%

no-peeping-toms's Introduction

no_peeping_toms

This plugin disables observers in your specs, so that model specs can run in complete isolation.

Installation

Add to your Gemfile:

gem 'no_peeping_toms', :git => 'git://github.com/alindeman/no_peeping_toms.git'

and run ‘bundle install`.

no_peeping_toms >= 2.0.0 only supports on Rails 3. If you need Rails 2 support, use 1.1.0: ‘gem install no_peeping_toms -v 1.1.0`

Usage

To disable observers, place the following code in your test.rb, or spec_helper.rb, or wherever:

ActiveRecord::Observer.disable_observers

You can easily reenable observers:

ActiveRecord::Observer.enable_observers

You can choose to run some code with specific observers turned on. This is useful when spec’ing an observer. For example, if you write the following observer:

class PersonObserver < ActiveRecord::Observer
  def before_update(person)
    old_person = Person.find person.id
    if old_person.name != person.name
      NameChange.create! :person => person, :old_name => old_person.name, :new_name => person.name
    end
  end
end

You can spec the Person class in complete isolation.

describe Person, " when changing a name" do
  before(:each) do
    @person = Person.create! :name => "Pat Maddox"
  end

  # By default, don't run any observers
  it "should not register a name change" do
    lambda { @person.update_attribute :name, "Don Juan Demarco" }.should_not change(NameChange, :count)
  end

  # Run only a portion of code with certain observers turned on
  it "should register a name change with the person observer turned on" do
    ActiveRecord::Observer.with_observers(:person_observer) do
      lambda { @person.update_attribute :name, "Don Juan Demarco" }.should change(NameChange, :count).by(1)
    end

    lambda { @person.update_attribute :name, "Man Without a Name" }.should_not change(NameChange, :count)
  end
end

Credits

  • Brandon Keepers

  • Corey Haines

  • Drew Olson

  • Jeff Siegel

  • Zach Dennis

  • Andy Lindeman

  • Ryan McGeary

Copyright © 2007-2011 Pat Maddox, released under the MIT license.

no-peeping-toms's People

Contributors

bkeepers avatar alindeman avatar jdsiegel avatar spraints avatar rmm5t avatar zdennis avatar

Stargazers

Alexander avatar

Watchers

Alexander 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.