我正在使用 serverspec 为每个主机编写测试。我的文件组织如下..
|-- Rakefile
`-- spec
|-- spec_helper.rb
`-- www.example.jp
`-- example_spec.rb
`-- spec_helper.rb
Rakefile的内容如下
require 'rake'
require 'rspec/core/rake_task'
task :spec => 'spec:all'
task :default => :spec
namespace :spec do
targets = []
Dir.glob('./spec/*').each do |dir|
next unless File.directory?(dir)
target = File.basename(dir)
target = "_#{target}" if target == "default"
targets << target
end
task :all => targets
task :default => :all
targets.each do |target|
original_target = target == "_default" ? target[1..-1] : target
desc "Run serverspec tests to #{original_target}"
RSpec::Core::RakeTask.new(target.to_sym) do |t|
ENV['TARGET_HOST'] = original_target
t.pattern = "spec/#{original_target}/*_spec.rb"
end
end
end
spec_helper.rb的内容如下
require 'highline/import'
if ENV['ASK_LOGIN_PASSWORD']
options[:password] = ask("\nEnter login password: ") { |q| q.echo = false}
else
options[:password] = ENV['LOGIN_PASSWORD']
end
set :ssh_options, options
我从 serverspec 获得了 spec_helper 的内容 - 高级技巧
我可以使用命令在本地机器上运行 example_spec.rb
/usr/bin/ruby -S rspec example_spec.rb
它工作正常。
但是当我尝试在 www.example.jp 上运行文件时(例如,将其作为主机名)这个主机,它失败了。
我只是使用以下命令运行。
rake spec:www.example.jp
我在另一台主机上也进行了相同的设置。
当我运行命令时,我得到
spec/spec_helper.rb:4:undefined local variable or method `options' for main:Object (NameError)
from spec/www.example.jp/example_spec.rb:1:in `require'
from spec/www.example.jp/example_spec.rb:1
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load_spec_files'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/exe/rspec:4
/usr/bin/ruby -I/usr/lib/ruby/gems/1.8/gems/rspec-support-3.2.2/lib:/usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/exe/rspec --pattern spec/blr2263728.idc.oracle.com/\*_spec.rb failed
我的 example_spec.rb 的内容是
require 'spec_helper'
describe command('whoami') do
its(:stdout) { should match('root')}
end
请告诉我我做错了什么??