运行 rspec 带“-e”参数

running rspec with "-e" parameter

我可以运行像

这样的简单ruby程序
ruby -e "puts 'raja'"

但是当我运行Rspec文件时,我运行这样

ruby rspec my_example_spec.rb

说,我有没有办法像我在第一行中那样将 rspec 程序作为参数传递?

我试过了

ruby -e rspec "require 'rspec'
require 'watir'
describe 'My behaviour' do
  it 'should do something' do
    b = Watir::Browser.new
    b.goto 'www.google.com'
    b.text_field(name: 'q').set 'Rajagopalan'
    sleep 2
    b.close
  end
end"

但这不是 运行宁。如何通过“-e”参数传递 rspec 程序?

Google 了一下,我在 RSpec github 上发现了这个问题线程:https://github.com/rspec/rspec-core/issues/359

看来您可以通过将

Ruby Ruby 中的 RSpec 测试
RSpec::Core::Runner::run({}, $stderr, $stdout)

在你的定义之后。

以下最小测试对我有效:

ruby -e "require 'rspec'
describe 'My behaviour' do
  it 'should do something' do
    expect(1).to eq(2)
  end
end
RSpec::Core::Runner::run({}, $stderr, $stdout)
"