RSpec拦截反叛,防止sudo?

时间:2014-07-16 17:08:08

标签: ruby rspec

RSpec拦截反叛,防止sudo?

我将一些代码从MiniTest移植到RSpec。所述代码现在失败了。

在IRB:

`sudo echo 1`   # returns 1

RSpec的:

# example.rb
class Example
  def self.doexec
    `sudo echo 1`
  end
end

# example_spec.rb
it '#doexec' do
  Example.doexec.should include("1")
end

......执行时:

rspec spec/lib/example_spec.rb
# spec fails, stderror:
sudo: no tty present and no askpass program specified

这是一个错误,功能或任何如何使用它的想法(没有硬连线密码)?

1 个答案:

答案 0 :(得分:0)

这是一种方法,您可以尝试: -

test.rb

#!/usr/bin/env ruby

class Example
  def self.do_this
    `echo <your password> | sudo -S echo 11`
  end
end

test_spec.rb

require_relative "../test.rb"

describe Example do
  it '#do_this' do
    expect(Example.do_this).to include("1")
  end
end

现在运行你的文件: -

arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
root's password:.

Finished in 0.07274 seconds (files took 0.13673 seconds to load)
1 example, 0 failures

阅读此post