Logo: Relish

  1. Sign in

Project: Rspec-role-mocks

Playing a role

Background
Given
a file named "spec/spec_helper.rb" with:
require 'rspec/role-mocks'
$LOAD_PATH.unshift('lib')
And
a file named "spec/roles/logger.rb" with:
RSpec.define_role 'Logger' do
  def log(message, opts = {}, &block); end
end
And
a file named "spec/console_logger_spec.rb" with:
require 'spec_helper'
require 'console_logger'
require 'roles/logger'

describe ConsoleLogger do
  it_plays_role 'Logger'
end
Scenarios
Object plays the role
Given
a file named "lib/console_logger.rb" with:
class ConsoleLogger
  def log(message, opts = {}, &block); end
end
When
I run rspec spec/console_logger_spec.rb
Then
the example should pass
Object does not play the role
Given
a file named "lib/console_logger.rb" with:
class ConsoleLogger
  def not_log(message); end
end
When
I run rspec spec/console_logger_spec.rb
Then
the example should fail
And
the output should contain:
ConsoleLogger does not play role "Logger" as expected
Object has other method arity
Given
a file named "lib/console_logger.rb" with:
class ConsoleLogger
  def log(message); end
end
When
I run rspec spec/console_logger_spec.rb
Then
the example should fail
And
the output should contain:
ConsoleLogger does not play role "Logger" as expected
Object has other method argument type
Given
a file named "lib/console_logger.rb" with:
class ConsoleLogger
  def log(message, opts = {}, no_block); end
end
When
I run rspec spec/console_logger_spec.rb
Then
the example should fail
And
the output should contain:
ConsoleLogger does not play role "Logger" as expected

Last published about 5 years ago by treppo.