-1
我有一個項目來創建模板ruby項目。如何在Serverspec/RSpec測試中調用bundler命令
我正在使用serverspec並希望驗證模板的行爲。
但是,使用command(`rake -T`)
失敗。如果我手動執行該命令,它按預期工作。
調試,當測試在Serverspec運行時,它發現了錯誤Gemfile
- 它是從我的項目(.
)使用的Gemfile,而不是生成目錄(target/sample_project
)。
如何通過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
你可以在命令中加入'cd target/sample_project && rake && cd -'嗎? – Kris
我試過了。我實際上已經添加了一個周圍的鉤子來改變當前目錄:'around(:example)do Dir.chdir(project_dir)end'。這按預期工作 - 我有另一個例子來檢查工作目錄是否符合預期。 – Tim
掛鉤可能無法工作,因爲將啓動一個新的子進程,該子進程可能沒有與父進程相同的當前工作目錄。你有沒有嘗試在命令中放入'cd',以便它在子進程的上下文中執行? – Kris