Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rspec-context-doubles

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rspec-context-doubles

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

rspec-context-doubles

Temporarily configure RSpec to use a different type of doubles.

Why would you want to do that?

Here is the one that resulted in this library:

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.

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.

FAQs

Package last updated on 22 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc