In RSpec 2, how can I start a process, run a few examples, and then kill this process?

I am trying to run some functional test on a small server that I created. I am running Ruby 1.9.2 and RSpec 2.2.1 on Mac OS X 10.6. I checked that the server is working correctly and does not cause the problems that I am experiencing. In my specification, I try to start a process to start a server, run a few examples, and then kill the process on which the server is running. Here is the code for my specification:

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 specification, it seems that the before (: all) and after (: all) blocks are running and the server processes are 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 on the call to Process.kill , the server starts and the tests start, but the server remains on, which means that I need to manually kill it.

It seems like I don't understand what the after (: all) method should do, because it does not start in the order I was thinking. Why is this happening? What I need to do to my specifications

+4
source share
1 answer

Are you sure the server is ready to accept connections? Perhaps something like this will 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 
+1
source

Source: https://habr.com/ru/post/1335870/


All Articles