psyho / bogus

Fake library for Ruby
Other
359 stars 14 forks source link

Spying on setter methods doesn't work #41

Closed DivineDominion closed 10 years ago

DivineDominion commented 10 years ago

When I call a method on my object, one of the object's collaborator's attributes will change. I wanted to test this exchange by spying on the attribute's setter method, i.e. #baz=. But since the usual have_received(:baz=) doesn't work (#40), I have to verify the expectation via have_received.baz=(any_args) which yields false positives all the time.

A working example with false positives:

require 'rubygems'
require 'rspec'
require 'bogus/rspec'

class Bar
  def baz=(x); end
end

class Foo
  attr_reader :barable
  def initialize(barable)
    @barable = barable
  end

  def forward_something(x)
    # barable.baz = x # disable to see whether the expectation works
  end
end

describe Foo do
  fake(:bar)
  it "forwards setting something" do
    foo = Foo.new(bar)
    foo.forward_something(:stuff)

    expect(bar).to have_received.baz=(any_args)
  end
end
DivineDominion commented 10 years ago

Nevermind. I just tried something stupid and it works:

expect(bar).to have_received.(:baz=, any_args)