6

I have a class, that in one situation should call :my_method, but in another situation must not call method :my_method. I would like to test both cases. Also, I would like the test to document the cases when :my_method should not be called.

Using any_instance is generally discouraged, so I would be happy to learn a nice way to replace it.

This code snippet is a reduced example on what I kind of test I would like to write.

class TestSubject
  def call
    call_me
  end

   def call_me; end
   def never_mind; end
end

require 'rspec'

spec = RSpec.describe 'TestSubject' do
  describe '#call' do
    it 'calls #call_me' do
      expect_any_instance_of(TestSubject).to receive(:call_me)
      TestSubject.new.call
    end

    it 'does not call #never_mind' do
      expect_any_instance_of(TestSubject).not_to receive(:never_mind)
      TestSubject.new.call
    end
  end
end

spec.run # => true

It works, but uses expect_any_instance_of method, which is not recommended.

How to replace it?

3 Answers 3

8

I'll do somehting like that

describe TestSubject do
  describe '#call' do
    it 'does not call #something' do 
      subject = TestSubject.new
      allow(subject).to receive(:something)

      subject.call

      expect(subject).not_to have_received(:something)
    end
  end
end

Hope this helped !

0
1

This is how I normally unit-test. I updated the code to support other possible questions you (or other readers) may have in the future.

class TestSubject
  def call
    some_call_me_value = call_me
    call_you(some_call_me_value)
  end

  def call_me; end
  def call_you(x); end
  def never_mind; end

  class << self
    def some_class_method_a; end

    def some_class_method_b(x, y); end
  end
end

require 'rspec'

spec = RSpec.describe TestSubject do
  context 'instance methods' do
    let(:test_subject) { TestSubject.new }

    describe '#call' do
      let(:args) { nil }
      let(:mocked_call_me_return_value) { 'somecallmevalue' }
      subject { test_subject.call(*args) }

      before do
        allow(test_subject).to receive(:call_me) do
          mocked_call_me_return_value
        end
      end

      it 'calls #call_me' do
        expect(test_subject).to receive(:call_me).once
        subject
      end

      it 'calls #call_you with call_me value as the argument' do
        expect(test_subject).to receive(:call_you).once.with(mocked_call_me_return_value)
        subject
      end

      it 'does not call #never_mind' do
        expect(test_subject).to_not receive(:never_mind)
        subject
      end

      it 'calls in order' do
        expect(test_subject).to receive(:call_me).once.ordered
        expect(test_subject).to receive(:call_you).once.ordered
        subject
      end
    end

    describe '#call_me' do
      let(:args) { nil }
      subject { test_subject.call_me(*args) }

      # it ...
    end

    describe '#call_you' do
      let(:args) { nil }
      subject { test_subject.call_you(*args) }

      shared_examples_for 'shared #call_you behaviours' do
        it 'calls your phone number'
        it 'creates a Conversation record'
      end

      # just an example of argument-dependent behaviour spec
      context 'when argument is true' do 
        let(:args) { [true] }

        it 'does something magical'
        it_behaves_like 'shared #call_you behaviours'
      end

      # just an example of argument-dependent behaviour spec
      context 'when argument is false' do
        let(:args) { [false] }

        it 'does something explosive'
        it_behaves_like 'shared #call_you behaviours'
      end
    end
  end

  context 'class methods' do
    let(:args) { nil }

    describe '#some_class_method_a' do
      let(:args) { nil }
      subject { TestSubject.some_class_method_a(*args) }

      # it ...
    end

    describe '#some_class_method_b' do
      let(:args) { [1, 2] }
      subject { TestSubject.some_class_method_b(*args) }

      # it ...
    end
  end
end

spec.run # => true
0

Do not test if some method was called or wasn't.
This will tight your tests to the implementation details and will force you to change tests every time you refactor(change implementation details without changing the behaviour) your class under test.

Instead test against return value or changed application state.
It is difficult come up with the example, you didn't provide enough context about the class under the test.

class CreateEntity
  def initialize(name)
    @name = name
  end 

  def call
    if company_name?(@name)
      create_company
    else
      create_person
    end
  end

  def create_person
    Person.create!(:name => @name)
  end

  def create_company
    Company.create!(:name => @name)
  end
end

# tests

RSpec.describe CreateEntity do
  let(:create) { CreateEntity.new(name).call }

  describe '#call' do
    context 'when person name is given' do
      let(:name) { 'Firstname Lastname' }
      it 'creates a person' do
        expect { create }.to change { Person.count }.by(1) 
      end

      it 'do not create a company' do
        expect { create }.not_to change { Company.count }
      end
    end

    context 'when company name is given' do
      let(:name) { 'Name & Sons Ltd' }
      it 'creates a company' do
        expect { create }.to change { Company.count }.by(1) 
      end

      it 'do not create a person' do
        expect { create }.not_to change { Person.count }
      end
    end
  end
end

With tests above I would be able to change how CreateEntity.call method implemented without changing tests as far as behaviour remain same.

3
  • 1
    Fabio, thanks for the comment. In general I agree that one should not test the implementation. However, in this case, the method in question is a call to an external resource, and I need absolute certainty when it is called, and when it is not called. This is an integral part of testing the effect of my code, and therefore I think testing it is useful and justified. Oct 22, 2018 at 8:33
  • @MatildaSmeds, then move call to external resource in the separated class, which you can mock in readable form.
    – Fabio
    Oct 22, 2018 at 8:48
  • The external resource is in a separable class. Even in that case, I want to mock a method call. I find the code given by @kevcha completely applicable for my case, and readable enough. Oct 22, 2018 at 8:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.