因此,我正在尝试运行测试,但出现错误提示。
Aruba::LaunchError:Command "seedly-calculator.rb" not found in PATH-variable
-seedly-calculator
-bin
-src
-seedly-calculator.rb
我试图更改rake文件中的路径,但是它不起作用。
我的seedly-calculator.rb
文件在根目录中。
require "rspec/core/rake_task"
namespace :spec do
desc "Run the functional suite against the CLI"
RSpec::Core::RakeTask.new(:functional, [] => [:set_path])
task :set_path do
project_bin_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'bin')
ENV['PATH'] = project_bin_dir + ':'+ ENV['PATH']
end
end
它显示如下错误:
Failure/Error: let(:command) { run "seedly-calculator.rb" }
Aruba::LaunchError:
Command "seedly-calculator.rb" not found in PATH-variable "/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/exe:/Users/bilaltariq/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/../bin:/Users/bilaltariq/.rbenv/versions/2.6.2/bin:/usr/local/Cellar/rbenv/1.1.1/libexec:/Users/bilaltariq/.rbenv/shims:/Users/bilaltariq/.asdf/shims:/Users/bilaltariq/.asdf/bin:/usr/local/bin:/Users/bilaltariq/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin".
我希望它能击中文件,所以我可以编写一些测试。 我在做错什么吗?
require 'spec_helper'
RSpec.describe 'Command Validation', type: :aruba do
let(:command) { run "seedly-calculator.rb" }
it "wrong/missing arguments" do
command.write("lookup\n")
stop_all_commands
expect(command.output).to end_with("Missing bank_name argument.\n")
end
结束
seedly-calculator.rb:
#!/usr/bin/env ruby
# Complete bin/setup so that after it is
# run, ruby seedly-calculator.rb can be used to launch
# it.
# frozen_string_literal: true
require_relative './src/runner'
if !ARGV.length.zero?
input = ARGV
Runner.new.send('process_input', input)
else
puts "Arguments required!."
end
答案 0 :(得分:0)
要使用run
运行ruby脚本,您需要确保ruby脚本是可执行文件并包含shebang,以便您的系统知道使用ruby运行它。这是this starter example
#!/usr/bin/env ruby
file = ARGV[0]
if file.nil? || file.empty?
abort "aruba-test-cli [file]: Filename is missing"
elsif !File.exist? file
abort "aruba-test-cli [file]: File does not exist"
end
puts File.read(file).chomp
因此,您需要将其添加到seedly-calculator.rb
文件的第一行
#!/usr/bin/env ruby
然后从命令行运行它以使其可执行。
chmod +x #!/usr/bin/env ruby
我举了一个简单的例子,分叉我上面提到的那一个。 See this commit
Rspec约定是它应与项目的相同文件结构匹配。手动设置PATH并不是一个好主意。
Rake任务通常放在任务文件夹中,因此您应在项目根目录中有一个任务文件夹
my_project/tasks/something.rake
然后您应该有一个匹配的spec文件夹
my_project/spec/tasks/something_spec.rb
这时,您应该可以摆脱task :set_path do end
块,而无需执行该规范即可。
您还应该有一个Gemfile来加载您的gem,运行bundle install
,然后使用
bundle exec rspec spec/tasks/sometask_spec.rb