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
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
精彩评论