test/unit/client_tests.rb in and-son-0.7.0 vs test/unit/client_tests.rb in and-son-0.8.0
- old
+ new
@@ -4,18 +4,18 @@
module AndSon::Client
class UnitTests < Assert::Context
desc "AndSon::Client"
setup do
- @current_timeout = ENV['ANDSON_TEST_MODE']
+ @current_test_mode = ENV['ANDSON_TEST_MODE']
ENV['ANDSON_TEST_MODE'] = 'yes'
@host = Factory.string
@port = Factory.integer
end
teardown do
- ENV['ANDSON_TEST_MODE'] = @current_timeout
+ ENV['ANDSON_TEST_MODE'] = @current_test_mode
end
subject{ AndSon::Client }
should have_imeths :new
@@ -125,26 +125,27 @@
setup do
@name = Factory.string
@params = { Factory.string => Factory.string }
@client = AndSon::TestClient.new(@host, @port)
- data = Factory.string
- @client.add_response(@name, @params){ data }
- @response = @client.responses.get(@name, @params)
end
subject{ @client }
- should have_accessors :timeout_value, :params_value, :logger_value
should have_readers :calls, :responses
- should have_imeths :add_response, :remove_response, :reset
+ should have_readers :before_call_procs, :after_call_procs
+ should have_accessors :timeout_value, :params_value, :logger_value
+ should have_imeths :add_response, :remove_responses, :reset
- should "default its params value" do
- assert_equal({}, subject.params_value)
+ should "know its calls and stored responses" do
+ assert_equal [], subject.calls
+ assert_instance_of AndSon::StoredResponses, subject.responses
end
- should "know its stored responses" do
- assert_instance_of AndSon::StoredResponses, subject.responses
+ should "default its params and callbacks" do
+ assert_equal({}, subject.params_value)
+ assert_equal [], subject.before_call_procs
+ assert_equal [], subject.after_call_procs
end
should "know its call runner" do
subject
end
@@ -156,41 +157,109 @@
call = subject.calls.last
assert_instance_of AndSon::TestClient::Call, call
assert_equal @name, call.request_name
assert_equal @params, call.request_params
- assert_equal @response.protocol_response, call.response
+ assert_instance_of Sanford::Protocol::Response, call.response
end
- should "return a stored response using `call`" do
- assert_equal @response.data, subject.call(@name, @params)
+ should "return a stored response's data using `call`" do
+ exp = subject.responses.get(@name, @params)
+ assert_equal exp.data, subject.call(@name, @params)
end
should "yield a stored response using `call` with a block" do
yielded = nil
subject.call(@name, @params){ |response| yielded = response }
- assert_equal @response.protocol_response, yielded
+ exp = subject.responses.get(@name, @params)
+ assert_equal exp.protocol_response, yielded
end
+ should "run before call procs" do
+ subject.params({ Factory.string => Factory.string })
+ yielded_name = nil
+ yielded_params = nil
+ yielded_runner = nil
+ subject.before_call do |name, params, call_runner|
+ yielded_name = name
+ yielded_params = params
+ yielded_runner = call_runner
+ end
+
+ subject.call(@name, @params)
+ assert_equal @name, yielded_name
+ exp = subject.params_value.merge(@params)
+ assert_equal exp, yielded_params
+ assert_same subject, yielded_runner
+ end
+
+ should "run after call procs" do
+ subject.params({ Factory.string => Factory.string })
+ yielded_name = nil
+ yielded_params = nil
+ yielded_runner = nil
+ subject.after_call do |name, params, call_runner|
+ yielded_name = name
+ yielded_params = params
+ yielded_runner = call_runner
+ end
+
+ subject.call(@name, @params)
+ assert_equal @name, yielded_name
+ exp = subject.params_value.merge(@params)
+ assert_equal exp, yielded_params
+ assert_same subject, yielded_runner
+ end
+
+ should "run callbacks in the correct order" do
+ n = 0
+ before_call_num = nil
+ after_call_num = nil
+ subject.before_call{ before_call_num = n += 1 }
+ subject.after_call{ after_call_num = n += 1 }
+
+ subject.call(@name, @params)
+ assert_equal 1, before_call_num
+ assert_equal 2, after_call_num
+ end
+
should "allow adding/removing stored responses" do
data = Factory.string
- subject.add_response(@name, @params){ data }
+ subject.add_response(@name).with(@params){ data }
response = subject.responses.get(@name, @params)
assert_equal data, response.data
- subject.remove_response(@name, @params)
+ subject.remove_responses(@name)
response = subject.responses.get(@name, @params)
assert_not_equal data, response.data
end
+ should "return a stored response stub using `add_response`" do
+ stub = subject.add_response(@name)
+ assert_instance_of AndSon::StoredResponses::Stub, stub
+
+ data = Factory.string
+ stub.with(@params){ data }
+ response = subject.responses.get(@name, @params)
+ assert_equal data, response.data
+
+ response = subject.responses.get(@name, {
+ Factory.string => Factory.string
+ })
+ assert_not_equal data, response.data
+ end
+
should "clear its calls and remove all its configured responses using `reset`" do
subject.call(@name, @params)
- assert_not_equal [], subject.calls
- assert_equal @response, subject.responses.get(@name, @params)
+ data = Factory.string
+ subject.add_response(@name).with(@params){ data }
+ assert_not_empty subject.calls
+ assert_equal data, subject.responses.get(@name, @params).data
+
subject.reset
- assert_equal [], subject.calls
- assert_not_equal @response, subject.responses.get(@name, @params)
+ assert_empty subject.calls
+ assert_not_equal data, subject.responses.get(@name, @params).data
end
should "be comparable" do
matching = AndSon::TestClient.new(@host, @port)
assert_equal matching, subject