Puppet rspec测试中的模拟方法

幼虫

我已经实现了一个自定义的Puppet函数,该函数查询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

我想在测试过程中模拟出authenticatelist_tenants方法,以便在没有实际的Keystone服务器的情况下测试其余的Puppet模块。

我以前从未使用过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

我希望能够从authenticatelist_tenants方法提供自定义返回(甚至从这些方法内部引发异常),以便我可以lookup_tenant在不同的失败情况下测试函数的行为

lackseal_90

WebMock可用于将http请求模拟为存根。这是github存储库的链接:https : //github.com/bblimke/webmock

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章