Rspec Mocks:从方法调用中模拟/产生块
我有这样的代码:
Net::SSH.start(@server, @username, :password => @password) do |ssh|
output = ssh.exec!(@command)
@logger.info 'SSH output: '
@logger.info output
end
我可以像这样使用 RSpec 的模拟框架来模拟 SSH.Start,告诉我我已经启动了 SSH 会话:
Net::SSH.should_receive(:start).with("server", "user", :password => "secret") do
@started = true
end
这告诉我是否已经 @started ssh 会话。现在我需要模拟 ssh.exec!方法,这很简单:
Net::SSH.should_receive(:exec!).with("command")...
但是我如何产生/调用包含 ssh.exec 的块!方法调用,因为我已经模拟了 SSH.start 方法?我可能可以调用一些简单的方法来执行这个块,但我不知道它是什么,也找不到关于 rspec 模拟框架的任何真正好的解释/文档。
I've got this code:
Net::SSH.start(@server, @username, :password => @password) do |ssh|
output = ssh.exec!(@command)
@logger.info 'SSH output: '
@logger.info output
end
I can mock the SSH.Start using RSpec's mock framework like this, to tell me that I've started the SSH session:
Net::SSH.should_receive(:start).with("server", "user", :password => "secret") do
@started = true
end
this tells me whether or not i've @started the ssh session. now I need to mock the ssh.exec! method, which is easy enough:
Net::SSH.should_receive(:exec!).with("command")...
but how do I yield / call the block that contains the ssh.exec! method call, since I have mocked the SSH.start method? there's probably some simple method I can call to execute this block, but I don't know what it is and can't find any really good explanations / documentation on rspec's mocking framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定为什么需要设置 @started,因为
should_receive
验证该方法已被调用。Not sure why you need to set
@started
, since theshould_receive
verifies that the method has been called.