Code Monkey home page Code Monkey logo

circumstance's Introduction

Circumstance

Circumstance allows you to register and evaluate setup blocks globally in your test suite. This is useful when you want to share big blocks of factory setups between tests.

Example

Circumstance ships with helpers which you can include in your suite to have access to helpers like load_circumstance. In RSpec you can do the following:

require 'circumstance'

RSpec.configure do |config|
  config.extend Circumstance::Helpers
end

Circumstance.define(:nuts_and_bolts) do
  let(:nuts_and_bolts) { create(:company, name: 'Nuts and Bolts') }
end

describe Company do
  load_circumstance :nuts_and_bolts
  
  it "has a name" do
    expect(nuts_and_bolts.name).to_not be_blank
  end
end

But you can also use it with other test suites.

require 'circumstance'

class ActiveSupport::TestCase
  extend Circumstance::Helpers
end

Circumstance.define(:large_database) do
  before do
    @number_count = 10_000
    @number_count.times do
      ActiveRecord::Base.connection.execute("INSERT INTO numbers SET i = 1")
    end
  end
end

class NumberTest < ActiveSupport::TestCase
  load_circumstance :large_database

  test "counts all numbers" do
    assert_equal @number_count, Number.count
  end
end

You can nest circumstances within other circumstances to reuse base circumstances in others.

    Circumstance.define(:company) do
      let(:company) { create(:company) }
    end

    Circumstance.define(:company_with_client) do
      load_circumstance :company
      let(:company_with_client) do
        company_with_client = company
        company_with_client.clients << create(:client)
        company_with_client 
      end
    end

The load_circumstance method tries to figure out if a circumstance was previously loaded so you won't load them multiple times for the same context. If you do need to evaluate a circumstance multiple times or directly on a test class for some reason, that's possible too.

Circumstance.evaluate(ActiveRecord::Base, :setup_mock_responses)

describe PunyHuman do
  Circumstance.evaluate(self, :setup_mock_responses)
  Circumstance.evaluate(self, :setup_mock_responses)
end

What's happening to my tests?

You can follow along with what Circumstance is doing with your circumstances by setting a different log level or changing the logger instance.

When logging to $stderr is good enough for you, just lower the log level.

Circumstance.logger.level = Logger::DEBUG

Alternatively you can swap out the entire logger.

Circumstance.logger = Rails.logger

Contributing

Circumstance is really tiny so any changes have big consequences. Please create an issue before hacking away at the code to discuss your changes. Thanks!

  1. Create a ticket on GitHub describing what you want to change and why you want to change it
  2. Discuss the changes
  3. Write the code
  4. Request a pull
  5. Bask in glory

License

The gem is available as open source under the terms of the MIT License.

circumstance's People

Contributors

manfred avatar mikeastock avatar oneill38 avatar

Stargazers

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

Watchers

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

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.