14

Let's say I have a class definition like so:

class Foo
  def init(val)
    @val = val
  end

  def self.bar
    :bar
  end

  def val
    @val
  end
end

with a spec like:

describe Foo
  it { should respond_to(:val) }
  it { should respond_to(:bar) }
end

The second it assertion fails. It isn't clear to me from RSpec's documentation that respond_to should fail on class methods.

michaelrshannon
  • 514
  • 3
  • 9
troutwine
  • 3,721
  • 3
  • 28
  • 62

2 Answers2

18

Nowadays it is suggested we use expect, like this:

describe Foo do
  it 'should respond to :bar' do
    expect(Foo).to respond_to(:bar)
  end
end

See: http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/


OLD ANSWER:

Actually you can make this approach by providing a subject:

describe Foo do
  subject { Foo }
  it { should respond_to :bar } # :bar being a class method
end

As described in here: http://betterspecs.org/#subject

Arthur Corenzan
  • 901
  • 9
  • 17
13

Your example should be written like this:

it 'should respond to ::bar' do
  Foo.should respond_to(:bar)
end
troutwine
  • 3,721
  • 3
  • 28
  • 62
Brandan
  • 14,735
  • 3
  • 56
  • 71