Module: Sinclair::Matchers

Defined in:
lib/sinclair/matchers.rb,
lib/sinclair/matchers/base.rb,
lib/sinclair/matchers/method_to.rb,
lib/sinclair/matchers/add_method.rb,
lib/sinclair/matchers/add_method_to.rb,
lib/sinclair/matchers/add_class_method.rb,
lib/sinclair/matchers/change_method_on.rb,
lib/sinclair/matchers/add_class_method_to.rb,
lib/sinclair/matchers/add_instance_method.rb,
lib/sinclair/matchers/change_class_method.rb,
lib/sinclair/matchers/add_instance_method_to.rb,
lib/sinclair/matchers/change_class_method_on.rb,
lib/sinclair/matchers/change_instance_method.rb,
lib/sinclair/matchers/change_instance_method_on.rb

Overview

Matchers module will have the DSL to be included in RSpec in order to have access to the matchers

Author:

  • darthjee

Defined Under Namespace

Modules: MethodTo Classes: AddClassMethod, AddClassMethodTo, AddInstanceMethod, AddInstanceMethodTo, AddMethod, AddMethodTo, Base, ChangeClassMethod, ChangeClassMethodOn, ChangeInstanceMethod, ChangeInstanceMethodOn, ChangeMethodOn

Instance Method Summary collapse

Instance Method Details

#add_class_method(method_name) ⇒ AddClassMethod

DSL to AddClassMethod

Examples:

Checking if a class had a class method added

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe 'MyBuilder' do
  let(:clazz)   { Class.new(MyModel) }

  let(:block) do
    proc do
      clazz.define_singleton_method(:new_method) { 2 }
    end
  end

  it do
    expect(&block).to add_class_method(:new_method).to(clazz)
  end
end

# outputs
# should add method class_method 'new_method' to #<Class:0x000055b4d0a25c80>

Returns:



38
39
40
# File 'lib/sinclair/matchers.rb', line 38

def add_class_method(method_name)
  Sinclair::Matchers::AddClassMethod.new(method_name)
end

#add_method(method_name) ⇒ AddInstanceMethod

DSL to AddInstanceMethod

Examples:

Using inside RSpec and checking an instance method being added

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe "MyBuilder" do
  let(:clazz)   { Class.new(MyModel) }
  let(:builder) { Sinclair.new(clazz) }

  before do
    builder.add_method(:new_method, "2")
  end

  it do
    expect { builder.build }.to add_method(:new_method).to(clazz)
  end
end

# Outputs
# 'should add method 'new_method' to #<Class:0x000056441bf46608> instances'

Using inside RSpec and checking instance

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe "MyBuilder" do
  let(:clazz)    { Class.new(MyModel) }
  let(:builder)  { Sinclair.new(clazz) }
  let(:instance) { clazz.new }

  before do
    builder.add_method(:the_method, "true")
  end

  it do
    expect { builder.build }.to add_method(:the_method).to(instance)
  end
end

# Outputs
# 'should add method 'the_method' to #<Class:0x000056441bf46608> instances'

Returns:



29
30
31
# File 'lib/sinclair/matchers.rb', line 29

def add_method(method_name)
  Sinclair::Matchers::AddInstanceMethod.new(method_name)
end

#change_class_method(method_name) ⇒ ChangeClassMethod

DSL to ChangeClassMethod

Examples:

Checking if a class method has changed

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe 'my test' do
  let(:builder) { Sinclair.new(klass) }
  let(:klass)   { Class.new(MyModel) }

  before do
    builder.add_class_method(:the_method) { 10 }
    builder.build
    builder.add_class_method(:the_method) { 20 }
  end

  it do
    expect{ builder.build }.to change_class_method(:the_method).on(klass)
  end
end

Returns:



56
57
58
# File 'lib/sinclair/matchers.rb', line 56

def change_class_method(method_name)
  Sinclair::Matchers::ChangeClassMethod.new(method_name)
end

#change_method(method_name) ⇒ ChangeInstanceMethod

DSL to ChangeInstanceMethod

Examples:

Checking if an instance method has changed

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe 'my test' do
  let(:builder) { Sinclair.new(klass) }
  let(:klass)   { Class.new(MyModel) }

  before do
    builder.add_method(:the_method) { 10 }
    builder.build
    builder.add_method(:the_method) { 20 }
  end

  it do
    expect{ builder.build }.to change_method(:the_method).on(klass)
  end
end

Checking if an instance method has changed on an instance

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe 'my test' do
  let(:builder)  { Sinclair.new(klass) }
  let(:instance) { klass.new }
  let(:klass)    { Class.new(MyModel) }

  before do
    builder.add_method(:the_method) { 10 }
    builder.build
    builder.add_method(:the_method) { 20 }
  end

  it do
    expect{ builder.build }.to change_method(:the_method).on(instance)
  end
end

Returns:



47
48
49
# File 'lib/sinclair/matchers.rb', line 47

def change_method(method_name)
  Sinclair::Matchers::ChangeInstanceMethod.new(method_name)
end