Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active April 27, 2016 15:26
Show Gist options
  • Save maxivak/e752ae6dc7986ef9bd4a to your computer and use it in GitHub Desktop.
Save maxivak/e752ae6dc7986ef9bd4a to your computer and use it in GitHub Desktop.
Rspec 3

RSpec3

Installation

in Gemfile:

group :test do
  gem 'rspec-rails', '3.1.0'
  gem 'rspec-activemodel-mocks'

  gem 'factory_girl_rails'
  gem 'ffaker'

  gem 'capybara'
  #gem "capybara-webkit"
  gem 'selenium-webdriver'
end

Check method is called

Stub method

obj.stub(:message) do |arg1, arg2|
  # set expectations about the args in this block
  # and set a return value
end


describe "a stubbed implementation" do
  it "works" do
    object = Object.new
    object.stub(:foo) do |arg|
      if arg == :this
        "got this"
      elsif arg == :that
        "got that"
      end
    end
    
    object.foo(:this).should eq("got this")
    object.foo(:that).should eq("got that")
  end
end


Verify arguments using block

RSpec.describe "Verifying arguments using a block" do
  it "fails when the arguments do not meet the expectations set in the block" do
    dbl = double

    allow(dbl).to receive(:foo) do |arg|
      expect(arg).to eq("bar")
    end

    dbl.foo(nil)
  end
end
  • for any_instance_of
        allow_any_instance_of(MyClass).to receive(:run) do |obj, arg1|
          expect(arg1).to match /bar /

        end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment