如何模擬ruby類的初始化方法?模擬ruby類的初始化方法?
我正在做一些測試,並想嘲笑從新調用創建的對象。
我試着寫了幾件事情,他們都沒有看到從新調用返回的模擬類。它只是不斷返回正常的,預期的對象。
編輯:
一個嘗試 -
class MockReminderTimingInfoParser < ReminderTimingInfoParser
def new(blank)
ReminderTimingInfoParserForTest.new
end
end
describe ReminderParser do
parser = ReminderParser.new(MockReminderTimingInfoParser)
it "should parse line into a Reminder" do
parser.parse(" doesnt matter \"message\"").should == Reminder.new('message', ReminderTimingInfo.new([DaysOfWeek.new([:sundays])], [1]))
end
end
class ReminderTimingInfoParserForTest
include TimingInfoParser
def parse_section(section); [DaysOfWeek.new([:sundays]), 1] end
def reminder_times_converter(times); times end
end
我很困惑。你想模擬'initialize'還是'new'? – 2010-09-15 23:25:04
是不是初始化給你新的方法?這是一個我不太瞭解的紅寶石細節。 – 2010-09-15 23:32:43
你是什麼意思「給你新''」?給你'new'的方法是'new',就像給你foobar的方法是'foobar'一樣。 – 2010-09-15 23:43:27