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

发布于 2024-10-12 17:58:41 字数 1316 浏览 2 评论 0原文

我正在尝试在我创建的小型服务器上运行一些功能测试。我在 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) 块,并且在运行示例之前服务器进程被终止,因为我得到以下输出:

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) 方法应该做什么,因为它没有按照我想象的顺序运行。为什么会发生这种情况?我需要做什么才能使我的规格

I am trying to run some functional test on a small server I have created. I am running Ruby 1.9.2 and RSpec 2.2.1 on Mac OS X 10.6. I have verified that the server works correctly and is not causing the problems I am experiencing. In my spec, I am attempting to spawn of a process to start the server, run some examples, and then kill the process running the server. Here is the code for my spec:

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

When I run the spec, it appears that the before(:all) and after(:all) blocks are run and the server processes is killed before the examples are run, because I get the following output:

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)>'

When I comment out the call to Process.kill, the server is started and the tests are run, but the server remains running, which means I have to go manually kill it.

It seems like I am misunderstanding what the after(:all) method is supposed to do, because it is not being run in the order I thought it would. Why is this happening? What do I need to do so that my specs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-10-19 17:58:41

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

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

Are you sure the server is ready to accept connections? Maybe something like this would help:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文