考慮以下兩個平凡型號:should_receive做我不指望的事嗎?
class Iq
def score
#Some Irrelevant Code
end
end
class Person
def iq_score
Iq.new(self).score #error here
end
end
而下面的Rspec的測試:
describe "#iq_score" do
let(:person) { Person.new }
it "creates an instance of Iq with the person" do
Iq.should_receive(:new).with(person)
Iq.any_instance.stub(:score).and_return(100.0)
person.iq_score
end
end
當我運行這個測試(或者說,類似的一個),它出現在短線有沒有工作:
Failure/Error: person.iq_score
NoMethodError:
undefined method `iq_score' for nil:NilClass
失敗,你可能會猜到,在上面標有「error here」的行上。當should_receive
行被註釋掉時,此錯誤消失。這是怎麼回事?
您是否嘗試刪除'with'方法或添加'and_return'方法? –
刪除'with'調用不起作用。假設'Iq'實例尚未創建,我將使用什麼作爲'和return'的參數? – hoffm
mock_model(Iq).as_null_object –