2014-02-28 81 views
1

我是一个红宝石新手,并试图让我的手在厨师肮脏。我已经写了一个关于postgresql社区食谱的封装食谱,并且希望使用测试厨房对它进行测试。以下是我写的spec.rb文件:需要serverspec中的红宝石库

require 'serverspec' 
require 'pg' 

include Serverspec::Helper::Exec 
include Serverspec::Helper::DetectOS 


RSpec.configure do |c| 
    c.before :all do 
    c.path = '/sbin:/usr/sbin' 
    c.os = backend(Serverspec::Commands::Base).check_os 
    end 
end 

describe "Postgresql server" do 
    it "should connect to database" do 
     conn = PG::Connection.open(:dbname => "db",:user => "user1",:password => "password") 
     conn.status == "CONNECTION_OK" 
    end 
end 

通过此测试,我希望检查用户和数据库是否已正确创建。 但是这个测试无法解决“pg”的依赖。我在哪里可以在serverspec中提到这种依赖关系? 我用kitchen verify [node name]来运行测试。

回答

2

创造必要的Ruby代码创业板安装需要在你的spec_helper.rb文件之前(或在规范文件的顶部,如果它更有意义):

begin 
    Gem::Specification.find_by_name('pg') 
rescue Gem::LoadError 
    require 'rubygems/dependency_installer' 
    Gem::DependencyInstaller.new(Gem::DependencyInstaller::DEFAULT_OPTIONS).install('pg') 
end 
require 'pg' 
+0

我使用运行此规范文件测试厨房。测试厨房负责在节点中安装所需的宝石。我不知道如何在测试厨房的生命周期中包含这个'bundle install'命令。 – Vaibhav

+0

我明白你的意思了。请参阅编辑的答案。 – cassianoleal

+0

我试过你的方法。测试厨房确实开始安装PG宝石。然而,它错误地出现“无法找到PostgreSQL客户端库(libpq)”。关于这个错误有很多讨论,我发现使用“env ARCHFLAGS =” - arch x86_64“gem install pg”解决了这个问题。我已经手动运行这个命令并且它成功了。但是,如何在DependencyInstaller中传递此参数? – Vaibhav