【发布时间】:2011-01-18 10:11:16
【问题描述】:
我有一个消息类,可以通过将参数传递给构造函数来初始化它,或者不传递参数然后稍后使用访问器设置属性。在属性的 setter 方法中进行了一些预处理。
我有一些测试可以确保 setter 方法做他们应该做的事情,但我似乎无法找到一个好的方法来测试 initialize 方法实际上调用了 setter。
class Message
attr_accessor :body
attr_accessor :recipients
attr_accessor :options
def initialize(message=nil, recipients=nil, options=nil)
self.body = message if message
self.recipients = recipients if recipients
self.options = options if options
end
def body=(body)
@body = body.strip_html
end
def recipients=(recipients)
@recipients = []
[*recipients].each do |recipient|
self.add_recipient(recipient)
end
end
end
【问题讨论】: