Skip to content

Instantly share code, notes, and snippets.

@porty
Last active January 3, 2016 10:29
Show Gist options
  • Save porty/8449576 to your computer and use it in GitHub Desktop.
Save porty/8449576 to your computer and use it in GitHub Desktop.
Ruby and/or Rails and/or RSpec snippets

Mock a static method on MyClass:

allow(MyClass).to receive(:my_method).with(1, 2, kind_of(Object)) { 'SOUNDS GOOD' }

To fail if it doesn't get called, use expect instead of allow

To mock a method on any instances of MyClass:

expect_any_instance_of(MyClass).to receive(:some_method) { 'OK THEN' }

An object that responds to things:

obj = double('optional-name').stub(get_five: 5, get_seven: 7)

How to stub a method with another method

expect_any_instance_of(AdyenNotificationsController).to receive(:capture_exception) do |e, extra|
  expect(e.message).to match(/Handling notifications of type BLAH is not implemented/)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment