Stub a setter on RSpec instance_double

mocking, rspec, ruby, unit-testing

Solution

Got it! The correct syntax looks like this:

allow(point).to receive(:coords=).and_return([4,2])

The `stub` method is apparently deprecated.

Problem

In an RSpec unit test I have a mock defined like this: ``` let(:point) { instance_double("Point", :to_coords => [3,2]) } ``` In the Point class I also have a setter, which is used in the class under test (which is called `Robot`). I would like to stub that setter to test `Robot#move`. Here's the wrong code I have so far: ``` describe "#move" do it "sets @x and @y one step forward in the direction the robot is facing" do point.stub(:coords=).and_return([4,2]) robot.move expect(robot.position).to eq([4,2]) end end ``` Here's the error message I get: `Double "Point (instance)" received unexpected message :stub with (:coords=)`

Original source