rubyrake-test

Stub repeated method calls for rake test


For my project I use rake test for testing my libs. For example, I've got a method, like connection.users.add_users(options)

json_response = check_users(options) 
batch = nil
Timeout::timeout(30) do
  begin
    sleep 1            
    batch = connection.batches.find(json_response["batch_id"])
  end while batch.state !="completed"
end
connection.users.add_users(batch.target_id, options)

So, first I make an HTTP request to my service, then I get the response (batch_id), loop until the batch is finished, then make another request and return the response.

Usually, in specs I do

let(:connection){setup_test_connection('{"batch_id": 344235}', '202')}

Which will stub connection's response, but in the case of this method it stubs only the first call and then tries to make a real request to my service and so I get an error (timeout because the service is actually down at that time).

Is there any way to stub every possible call of connection's class methods?


Solution

  • So i found out.

    I should have used stubs to fake inside requests like this:

    connection.servers.stubs(:schedule_create).returns({"batch_id" => 235234})