timting
timting

Reputation: 57

Test that instance method calls class method

Simply put, I have a class with an instance method which calls a class method, and I want to test using RSpec that the class method is being called when I run the instance method. So, for example

class Test
  def self.class_method
    #do something
  end

  def instance_method
    #do stuff
    self.class.class_method
  end
end

In RSpec, I've tried Test.should_receive(:class_method), but that seems to do something weird causing my tests to return weird behavior. If that is what I'm supposed to be using, maybe RSpec is out of date? I'm using RSpec 2.7.0. Thanks!

Upvotes: 0

Views: 540

Answers (1)

Peter Brown
Peter Brown

Reputation: 51697

If you don't really care what the class method is doing, and only that it gets called, you can do something like:

describe Test do
  context '#instance_method' do
    it 'should call the class method' do
      Test.should_receive(:class_method)
      Test.new.instance_method
    end
  end
end

Upvotes: 1

Related Questions