2

RSpec doubles can not be changed by extending its instances.

Minimal example

Please note that the example described here is just a minimal example to demonstrate the problem. The original classes are more complex and the behavior that is specced (obj.extend(Something)) is needed and can not be changed.

Spec

Let us have a look at the spec first. In the following example you can see the spec how I'd like it to look like:

require 'spec_helper'

RSpec.describe Modifier do
  subject(:modifier) { described_class.new(active) }

  describe "#apply_to(obj)" do
    subject(:obj) { instance_double(Foo, foo: "foo") }

    before { modifier.apply_to(obj) }

    context "when active" do
      let(:active) { true }
      its(:foo) { is_expected.to eq("extended-foo") }  # NOTE: This one will fail
    end

    context "when not active" do
      let(:active) { false }
      its(:foo) { is_expected.to eq("foo") }
    end
  end
end

Unfortunately, this is not working :(

Failures:

  1) Modifier#apply_to(obj) when active foo should eq "extended-foo"
     Failure/Error: its(:foo) { is_expected.to eq("extended-foo") }

       expected: "extended-foo"
            got: "foo"

Foo

class Foo
  attr_reader :foo

  def initialize(foo)
    @foo = foo
  end
end

Modifier

This class will modify given objects by extending its instance methods.

class Modifier
  def initialize(active)
    @active = active
  end

  # NOTE: This is the interesting method
  def apply_to(obj)
    return unless active?

    obj.extend(Extended)     # NOTE: And this is the interesting LOC
  end

  def active?
    @active
  end

  private

  attr_reader :active

  module Extended
    def foo
      "extended-#{super}"
    end
  end
end

Even more minimal example

The whole problem can be broken down to the following code snippet:

module Bar
  def foo
    "bar"
  end
end

double = RSpec::Mocks::Double.new("Foo", foo: "foo")
obj = Object.new

double.extend(Bar)
obj.extend(Bar)

double.foo
# => "foo"

obj.foo
# => "bar"

Conclusion

RSpec doubles can not be changed by extending its instances, which is expected behavior?

If so, how can you create readable specs for the example described here?

Links

6
  • Why not just subject(:obj) { Foo.new('foo') }?
    – Stefan
    Nov 30, 2015 at 11:23
  • Because this is a simplified example. In reality it is expensive to create this object. It would look more like: Foo.new(lot, of, complex, arguments) with a lot of dependencies. That's why I want to mock Foo.
    – Deradon
    Nov 30, 2015 at 11:32
  • 1
    Instead of mocking the real Foo, you could create an unnamed class via Class.new { ... }
    – Stefan
    Nov 30, 2015 at 13:27
  • @Stefan That is exactly the solution I am going with right now. Unfortunately than we will lose the ability to verify our doubles. (instance_double will verify that the interface you mock exists. So you can not stub non-existing methods. E.g.: instance_double(Foo, bar: "bar") will fail.)
    – Deradon
    Nov 30, 2015 at 14:51
  • But you are testing Modifier, not Foo. Why do have to verify Foo's interface at that point?
    – Stefan
    Nov 30, 2015 at 15:02

1 Answer 1

0

If the spec knows that it expexts an extendend object, why not expect the extend call itself?

expect(obj).to receive(:extend).and_return { class.new.extend(...) }
1
  • I consider the extend implementation detail. The same behavior can be achieved by: def obj.foo; "extended-#{super}"; end By changing the implementation here, the specs would break :(
    – Deradon
    Nov 30, 2015 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.