psyho / bogus

Fake library for Ruby
Other
359 stars 14 forks source link

RSpec described_class and verify_contract problems #21

Closed bernd closed 11 years ago

bernd commented 11 years ago

When using verify_contract in a test for a class, using described_class doesn't work anymore because the returned value still refers to the original class and not the proxy object.

require 'bogus/rspec'

class Engine
  def start
  end
end

class Car < Struct.new(:engine)
  def start
    engine.start
  end
end

describe Car do
  fake(:engine)

  let(:car) { Car.new(engine) }

  describe '#start' do
    it 'starts the engine' do
      car.start

      expect(engine).to have_received.start
    end
  end
end

describe Engine do
  verify_contract(:engine)

#  let(:engine) { Engine.new }
  let(:engine) { described_class.new }

  describe '#start' do
    it 'returns true' do
      expect(engine.start).to be_nil
    end
  end
end

The above code throws the following error. When Engine is used instead of described_class it works.

$ rspec /tmp/bogus-repro.rb 
..

Finished in 0.00706 seconds
2 examples, 0 failures
/.rvm/gems/ruby-1.9.3-p448/gems/bogus-0.1.0/lib/bogus/verifies_contracts.rb:14:in `verify': Contract not fullfilled for engine! (Bogus::ContractNotFulfilled)

Missed interactions:
  - #start()

Actual interactions:

    from /.rvm/gems/ruby-1.9.3-p448/gems/bogus-0.1.0/lib/bogus/public_methods.rb:8:in `verify_contract!'
    from /.rvm/gems/ruby-1.9.3-p448/gems/bogus-0.1.0/lib/bogus/rspec_extensions.rb:19:in `block (2 levels) in verify_contract'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:470:in `instance_eval'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:470:in `instance_eval_with_rescue'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:31:in `run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `block in run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `each'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:30:in `block in run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from /.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

Not sure if this can be fixed in a nice way, but at least a warning in the documentation would be nice.

psyho commented 11 years ago

Looks like we are not overwriting described_class. I'll try to fix this before the next release. Thanks for reporting the bug.