Giter Site home page Giter Site logo

rspec-context-doubles's Introduction

rspec-context-doubles

Temporarily configure RSpec to use a different type of doubles.

Why would you want to do that?

If you use Rails and add a helper method from a controller (or use a gem like canable that does), RSpec will raise an error when one of those helper methods is stubbed in a view spec if it is configured to use verifying doubles.

...or any other situation where you want doubles behavior that is different than the project's configuration.

How do you use it?

Just pass :normal_doubles or :verifying_doubles as metadata to a describe block and you'll temporarily set the behavior for the examples in that block.

# spec_helper.rb
# ...
config.mock_with :rspec do |mocks|
  mocks.verify_partial_doubles = true
end
# ...
# Gemfile
group :test do
  gem "rspec-context-doubles"
end
# example.rb
class Example
  def foo; end
end
# example_spec.rb
describe Example do
  context "configured to use verifying doubles" do
    before(:all) { RSpec::Mocks.configuration.verify_partial_doubles = true }

    describe "temporarily using normal doubles", :normal_doubles do
      it "does not raise an error when a non-existent method is stubbed" do
        expect{allow(subject).to receive(:fo)}.to_not raise_error
      end
    end
    it "raises an error when a non-existent method is stubbed" do
      expect{allow(subject).to receive(:fo)}.to raise_error(RSpec::Mocks::MockExpectationError)
    end
  end

  context "configured to use normal doubles" do
    before(:all) { RSpec::Mocks.configuration.verify_partial_doubles = false }

    describe "temporarily using verifying doubles", :verifying_doubles do
      it "raises an error if a non-existent method is stubbed" do
        expect{allow(subject).to receive(:fo)}.to raise_error(RSpec::Mocks::MockExpectationError)
      end
    end
    it "does not raise an error when a non-existent method is stubbed" do
      expect{allow(subject).to receive(:fo)}.to_not raise_error
    end
  end
end

Credits

Written by @barelyknown.

This gem uses RSpec's shared context feature.

The idea for the gem came from this rspec-mocks issue.

rspec-context-doubles's People

Contributors

barelyknown avatar

Watchers

 avatar James Cloos 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.