2017-03-07 15 views
-1

私はテンプレートRubyプロジェクトを作成するプロジェクトを持っています。Serverspec/RSpecテストからbundlerコマンドを呼び出す方法

私はserverspecを使用しており、テンプレートの動作を確認する必要があります。

ただし、command(`rake -T`)を使用すると失敗します。手動でコマンドを実行すると、期待どおりに動作します。

デバッグ、テストがServerspecで実行されている場合、それは間違ってGemfile見つけた - それは、私のプロジェクト(.)からではなく、生成ディレクトリ(target/sample_project)をGemfileを使用しています。

Serverspec/Rspecテストでrakeまたはbundlerコマンドを呼び出すにはどうすればよいですか?

サンプルコード:

require "spec_helper" 
require 'serverspec' 
require 'fileutils' 

set :backend, :exec 
set :login_shell, true 

describe "Generated Template" do 
    output_dir='target' 
    project_dir="#{output_dir}/sample_project" 

    # Hooks omitted to create the sample_project 
    # and change working directory to `project_dir` 

    describe command('rake -T') do 
    its(:stdout) { should include "rake serverspec:localhost" } 
    its(:stdout) { should include "rake serverspec:my_app" } 
    end 
end 
+0

コマンドに 'cd target/sample_project && rake && cd-'を追加できますか? – Kris

+0

私はそれを試みました。私は実際に、現在のディレクトリを変更するためにaroundフックを追加しました。 'around(:example)do Dir.chdir(project_dir)end'です。これは期待どおりに動作します - 私は、作業ディレクトリが期待どおりであることを確認する別の例があります。 – Tim

+0

新しい子プロセスが起動されるため、親プロセスと同じ現在の作業ディレクトリがない可能性があるため、フックが機能しない可能性があります。子プロセスのコンテキストで実行されるように 'cd'をコマンドの中に入れようとしましたか? – Kris

答えて

0

バンドラーは、ここに文書化され、外部シェルコマンドを実行するための規定を持っていますバンドラ/すくいタスクを実行http://bundler.io/v1.3/man/bundle-exec.1.html

ではなくServerspecのBundler.with_clean_envを使用してRSpecのを、使用可能です。

require 'bundler' 
require 'rspec' 
RSpec.describe "Generated Template" do 

    output_dir='target' 
    project_dir="#{output_dir}/sample_project" 

    around(:example) do |example| 
    #Change context, so we are in the generated project directory 
    orig_dir=Dir.pwd 

    Dir.chdir(project_dir) 
    example.run 
    Dir.chdir(orig_dir) 

    end 

    around(:example) do |example| 
    Bundler.with_clean_env do 
     example.run 
    end 
    end 

    it "should include localhost" do 
    expect(`rake -T 2>&1`).to include "rake serverspec:localhost" 
    end 
end 
関連する問題