Logo: Relish

  1. Sign in

Project: Rspec-role-mocks

Initialize mocks with allowed calls

Scenarios
mock allows the predefined calls and returns value
Given
a file named "spec/roles/fetcher.rb" with:
RSpec.define_role 'Fetcher' do
  def fetch(id) end
end
And
a file named "spec/spec_helper.rb" with:
require 'rspec/role-mocks'
$LOAD_PATH.unshift("lib")
Given
a file named "lib/user.rb" with:
class User < Struct.new(:fetcher)
  def favorite_artist
    fetcher.fetch(1)
  end
end
And
a file named "spec/user_spec.rb" with:
require 'spec_helper'
require 'user'
require 'roles/fetcher'

RSpec.describe User, '#suspend!' do
  it 'notifies the console' do
    expected = 'Theolonious Monk'
    fetcher = role_double("Fetcher", fetch: expected)
    user = User.new(fetcher)

    actual = user.favorite_artist

    expect(actual).to eq expected
  end
end
When
I run rspec spec/user_spec.rb
Then
the example should pass

Last published about 5 years ago by treppo.