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
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)
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
source share