我有一个在测试中使用$?.exitstatus的Rails应用程序,它运行得很好。
我正在尝试制作一个在测试中使用$?.exitstatus的gem,但我收到了以下消息:
NoMethodError:
undefined method `exitstatus' for nil:NilClass下面是我为再现问题而创建的两个示例文件:
superman.rb
class Superman
def self.execute(command)
`#{command}`
ensure
puts $?.exitstatus
end
endmy_spec.rb
describe Superman do
it 'should execute' do
expect(Superman).to receive(:execute).and_return(1)
expect($?.exitstatus).to be 0
end
end为什么我可以在Rails规范中使用$? ,而不能在普通的红宝石中使用?我需要什么东西吗?
发布于 2016-03-08 18:48:54
$?返回要执行的最后一个子进程的Process::Status。如果没有执行子进程,您将得到nil。
在这种情况下,由于您实际上没有调用Superman.execute,因此不需要返回子进程状态。此外,即使您将Superman.execute("ls")添加到您的规范中,您也在上面添加了它,并且同样的情况仍然成立。
尝试:
describe Superman do
it 'should execute' do
# There's really no reason for this expect(..).to receive anyway
# since it's pretty obvious it's going to get called since we're
# calling it directly right below.
expect(Superman).to receive(:execute).and_call_original
Superman.execute("ls")
expect($?.exitstatus).to eq(0)
end
end产量:
rspec ./superman_spec.rb
0
.
Finished in 0.01054 seconds (files took 0.10346 seconds to load)
1 example, 0 failureshttps://stackoverflow.com/questions/35875338
复制相似问题