我需要允许一个类在Rspec/Rspec-mocks中接收另一个类的实例作为参数。
上下文是我正在尝试为Puppet(即Ruby应用程序(创建Rspec测试。
我想在 Rspec 中说"类似的东西"——
allow(Puppet::FileServing::Content.indirection).
to receive(:find).with(
"puppet:///modules/profile/logstash/logstash.json",
an_instance # an instance of Puppet::Node::Environment would go here
).and_return('bar')
创建类的假实例似乎不是一种选择,因为接收的实例取决于运行期间计算的值。
因此,在我的 Pry 调试器中,我看到以下内容:
=> 99: Puppet::FileServing::Content.indirection.find(
100: self[:source],
101: :environment => catalog.environment_instance
102: )
和
[1] pry(#<Puppet::Type::Elasticsearch_template>)> catalog.environment_instance
=> <Puppet::Node::Environment:70286646297160 @name="rp_env" @manifest="/Users/alexharvey/git/elk/spec/fixtures/manifests" @modulepath="/Users/alexharvey/git/elk/spec/fixtures/modules" >
我陷入困境的地方是我不知道如何告诉测试"允许"Puppet::Node::Environment 的实例接收我在调试器中看到的内容。
例如,我不知道字符串"70286646297160"是什么(我想它以某种方式标识了类的实例?
而且我不能参考这些路径,因为它们特定于我的笔记本电脑,我正在编写的测试只能在我的笔记本电脑上运行。
那么,我如何"告诉"Rspec"允许"接收我在调试器中看到的这个类实例,假设可以这样做。
(我希望这是有道理的。显然,这有点超出我的Rspec/Ruby知识。
您可以将hash_including
与instance_of
匹配器一起使用(而不是示例中写为an_instance
(:
hash_including(environment: instance_of(Puppet::Node::Environment))