Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub a setter on RSpec instance_double

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=)

like image 523
niftygrifty Avatar asked Jul 06 '14 00:07

niftygrifty


1 Answers

Got it! The correct syntax looks like this:

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

The stub method is apparently deprecated.

like image 182
niftygrifty Avatar answered Sep 24 '22 09:09

niftygrifty