Samuel-Zacharie Faure
Samuel-Zacharie Faure

Reputation: 1152

Rspec not finding class methods

I'm writing some tests for my backend jobs and I'm having a weird issue with rspec not finding my methods.

I wrote a simple class & test to illustrate the issue :

app/interactors/tmp_test.rb :

class TmpTest
  def call
    a = 10
    b = 5
    b.substract_two
    return a + b
  end

  def substract_two
    c = self - 2
    return c
  end
end

spec/interactors/tmp_test.rb :

require 'rails_helper'

describe TmpTest do
  context 'when doing the substraction' do
    it 'return the correct number' do
      expect(described_class.call).to eq(13)
    end
  end
end

output:

TmpTest
  when doing the substraction
    return the correct number (FAILED - 1)

Failures:

  1) TmpTest when doing the substraction return the correct number
     Failure/Error: expect(described_class.call).to eq(13)

     NoMethodError:
       undefined method `call' for TmpTest:Class
     # ./spec/interactors/tmp_test.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.00177 seconds (files took 1.93 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/interactors/tmp_test.rb:5 # TmpTest when doing the substraction return the correct number

Upvotes: 1

Views: 2438

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

This is a complete mess. Corrected version with comments:

class TmpTest
  def call
    a = 10
    b = 5
    # b.substract_two # why do you call method of this class on b?!
    a + subtract_two(b)
  end

  def substract_two(from)
    from - 2
  end
end

Also: don’t use return in the very last line of the method.

Upvotes: -1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

It's not a class method, it's an instance method. Your test should look like this:

describe TmpTest do
  subject(:instance) { described_class.new }

  context 'when doing the subtraction' do
    it 'returns the correct number' do
      expect(instance.call).to eq(13)
    end
  end
end

Upvotes: 2

Related Questions