我有以下方法来创建一个新的 Connection 对象。它将打开一个串行端口。请注意,当端口不存在时,它将失败。
class Connection
def initialize(port)
begin
@serial = SerialPort.new(port, 9600, 8, 1, SerialPort::NONE)
rescue
exit(1)
end
end
def send_command
@serial.write "Something"
end
end
我为这个方法写了一个 RSpec 规范,到目前为止一切都很好。现在,我想指定下一个方法“send_command”。
问题是我不能调用Connection.new("/some/port")
这个规范,因为它会失败(端口不存在)。如何绕过创建方法而不存根新方法?如果我理解正确,我不允许对我正在测试的课程进行存根或模拟,对吗?
谢谢!