gpt4 book ai didi

ruby - 在 RSpec 2 中,我如何产生一个进程,运行一些示例然后终止该进程?

转载 作者:数据小太阳 更新时间:2023-10-29 07:38:37 26 4
gpt4 key购买 nike

我正在尝试在我创建的小型服务器上运行一些功能测试。我在 Mac OS X 10.6 上运行 Ruby 1.9.2 和 RSpec 2.2.1。我已经验证服务器工作正常并且没有导致我遇到的问题。在我的规范中,我试图生成一个进程来启动服务器,运行一些示例,然后终止运行服务器的进程。这是我的规范的代码:

describe "Server" do
describe "methods" do

let(:put) { "put foobar beans 5\nhowdy" }

before(:all) do
@pid = spawn("bin/server")
end

before(:each) do
@sock = TCPSocket.new "127.0.0.1", 3000
end

after(:each) do
@sock.close
end

after(:all) do
Process.kill("HUP", @pid)
end

it "should be valid for a valid put method" do
@sock.send(put, 0).should == put.length
response = @sock.recv(1000)
response.should == "OK\n"
end

#more examples . . .

end
end

当我运行规范时,似乎运行了 before(:all) 和 after(:all) block ,并且服务器进程在示例运行之前被终止,因为我得到以下输出:

F

Failures:

1) Server methods should be valid for a valid put method
Failure/Error: @sock = TCPSocket.new "127.0.0.1", 3000
Connection refused - connect(2)
# ./spec/server_spec.rb:11:in `initialize'
# ./spec/server_spec.rb:11:in `new'
# ./spec/server_spec.rb:11:in `block (3 levels) in <top (required)>'

当我注释掉对 Process.kill 的调用时,服务器启动并运行测试,但服务器仍在运行,这意味着我必须手动终止它。

我好像误解了 after(:all) 方法应该做什么,因为它没有按照我认为的顺序运行。为什么会这样?我需要做什么才能使我的规范

最佳答案

您确定服务器已准备好接受连接吗?也许这样的事情会有所帮助:

before(:each) do
3.times do
begin
@sock = TCPSocket.new "127.0.0.1", 2000
break
rescue
sleep 1
end
end
raise "could not open connection" unless @sock
end

关于ruby - 在 RSpec 2 中,我如何产生一个进程,运行一些示例然后终止该进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com