【发布时间】:2014-04-18 16:00:28
【问题描述】:
我已经实现了一个自定义 Puppet 函数,它可以查询 Keystone 服务器以获取信息。定义此函数的模块包括一些辅助方法,它们执行查询 keystone 的实际工作。概括地说,结构如下所示:
def authenticate(auth_url, username, password)
...
end
def list_tenants(auth_url, token)
...
end
module Puppet::Parser::Functions
newfunction(:lookup_tenant, :type => :rvalue) do |args|
...
end
end
我想模拟出 authenticate 和 list_tenants 方法
在测试期间,以便我可以测试 Puppet 模块的其余部分
没有实际的 Keystone 服务器。
我以前没有使用过 Ruby 或 Rpsec,现在我 很难找到如何为这些提供存根的示例 内部方法。
到目前为止,我有一个存根 rspec 文件,它简单地验证了 功能:
require 'spec_helper'
describe 'lookup_tenant' do
it "should exist" do
Puppet::Parser::Functions.function("lookup_tenant").should == "function_lookup_tenant"
end
# This will fail because there is no keystone server.
it "should fail" do
should run.with_params(
'http://127.0.0.1:35357/v2.0',
'admin_user',
'admin_password',
'admin_tenant_name',
'target_tenant_name'
).and_raise_error(KeystoneError)
end
end
我希望能够从
authenticate 和 list_tenants 方法(甚至引发异常
从这些方法内部),以便我可以测试
lookup_tenant 不同故障场景下的函数。
【问题讨论】:
-
您可以尝试使用 WebMock gem,它提供了以任何形式模拟和存根 http 调用...github.com/bblimke/webmock
-
webmock gem 很漂亮,完全解决了我的问题。您应该将其作为答案而不是评论。
标签: ruby unit-testing rspec puppet