2014-07-06 42 views
3

設定器在一個RSpec單元測試我已經定義像這樣的模擬:存根上的RSpec instance_double

let(:point) { instance_double("Point", :to_coords => [3,2]) } 

在點I類也有setter,它是在類下試驗中使用(這是稱爲Robot)。我想存根二連擊測試Robot#move。這是錯誤的代碼,我到目前爲止有:

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 

這裏的錯誤消息我得到:

Double "Point (instance)" received unexpected message :stub with (:coords=)

回答

11

明白了!正確的語法如下:

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

stub方法顯然是過時了。

0

另一種選擇是存根setter方法在雙的定義,像這樣:

let(:point) { double("point", 'coords=' => [4,2]) } 

詳見this github issue