0

There are several cases, where my specs always return true, making the test superfluous...

Take this Achievement model for instance:

class Achievement < ActiveRecord::Base
  has_many :stages

  def call_name_method
    name
  end

  def name
    key
  end
end

# for simplicity sake, I'm just testing a method which simply calls another method.

achievement_specs.rb

require 'rails_helper'

describe Achievement do
  describe '#call_name_method' do
    subject { achievement.call_name_method }
    let(:achievement) { create(:achievement) }

    it 'calls #name' do
      expect(achievement).to receive(:name)
      subject
    end
  end
end

This test succeeds, but I can change it to expect(achievement).to receive(:foobar) and it will still succeed, although I am not calling foobar.

According to this answer, it is the correct syntax, but it somehow never fails. Is this a bug?

I also tried using .to have_received(:call_name_method), but that results in this error:

1) Achievement#call_name_method calls #name Failure/Error: expect(achievement).to have_received(:call_name_method) # expected to have received call_name_method, but that object is not a spy or method has not been stubbed.

Community
  • 1
  • 1
davegson
  • 8,205
  • 4
  • 51
  • 71
  • achievement.stages is a relation; also Stage does not have a method `register!`. What exactly do you want to test? – bosskovic Nov 26 '15 at 12:33
  • I want to test if the `register!` method of Achievement calls the `register!` method on its stages, and I added the Stage model with its method – davegson Nov 26 '15 at 12:39
  • what is achievement in subject { achievement.call_name_method } ? Do you need to override the achievement in the next line? – bosskovic Nov 26 '15 at 13:20
  • achievement is a factory, which does get defined the line after yes. I could also switch the lines, that does not make a difference – davegson Nov 26 '15 at 13:21
  • The code looks fine. The error must be elsewhere. If you provided a minimal example that we could run, that'd be perfect. – Sergio Tulentsev Nov 26 '15 at 13:24
  • Hmm maybe you are right... I'll try and reproduce it on a fresh app... – davegson Nov 26 '15 at 13:24

1 Answers1

0

The problem was in the rails_helper.rb

config.after(:each) do
  DatabaseCleaner.clean

  RSpec::Mocks.teardown # <-- this line was fault
end

After removing that line the specs worked as expected. A coworker accidentally commited this although it was not needed.

Since all test were 'succeeding', it did not catch our attention until now.

davegson
  • 8,205
  • 4
  • 51
  • 71