Logo: Relish

  1. Sign in

Project: Rspec-role-mocks

Returning a value

Background
Given
a file named "spec/roles/notifier.rb" with:
RSpec.define_role 'Notifier' do
  def notify; end
end
And
a file named "spec/spec_helper.rb" with:
require 'rspec/role-mocks'

$LOAD_PATH.unshift("lib")
Scenarios
Return nil by default
Given
a file named "spec/user_spec.rb" with:
require 'spec_helper'
require 'roles/notifier'

RSpec.describe 'Default return value' do
  it 'notifies the console' do
    notifier = role_double("Notifier")

    expect(notifier).to receive(:notify)

    expect(notifier.notify).to be_nil
  end
end
When
I run rspec spec/user_spec.rb
Then
the examples should all pass
Specify a return value
Given
a file named "spec/user_spec.rb" with:
require 'spec_helper'
require 'roles/notifier'

RSpec.describe 'Specified return value' do
  it 'notifies the console' do
    notifier = role_double("Notifier")

    expect(notifier).to receive(:notify).and_return(1)

    expect(notifier.notify).to eq(1)
  end
end
When
I run rspec spec/user_spec.rb
Then
the examples should all pass

Last published about 5 years ago by treppo.