如何模拟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