test/unit/message_handler_tests.rb in qs-0.5.0 vs test/unit/message_handler_tests.rb in qs-0.6.0
- old
+ new
@@ -1,15 +1,24 @@
require 'assert'
require 'qs/message_handler'
+require 'much-plugin'
+require 'test/support/message_handler'
+
module Qs::MessageHandler
class UnitTests < Assert::Context
+ include Qs::MessageHandler::TestHelpers
+
desc "Qs::MessageHandler"
setup do
+ Qs.init
@handler_class = Class.new{ include Qs::MessageHandler }
end
+ teardown do
+ Qs.reset!
+ end
subject{ @handler_class }
should have_imeths :timeout
should have_imeths :before_callbacks, :after_callbacks
should have_imeths :before_init_callbacks, :after_init_callbacks
@@ -19,10 +28,14 @@
should have_imeths :before_run, :after_run
should have_imeths :prepend_before, :prepend_after
should have_imeths :prepend_before_init, :prepend_after_init
should have_imeths :prepend_before_run, :prepend_after_run
+ should "use much-plugin" do
+ assert_includes MuchPlugin, Qs::Worker
+ end
+
should "allow reading/writing its timeout" do
assert_nil subject.timeout
value = Factory.integer
subject.timeout(value)
assert_equal value, subject.timeout
@@ -145,46 +158,94 @@
end
class InitTests < UnitTests
desc "when init"
setup do
- @runner = FakeRunner.new
- @handler = TestMessageHandler.new(@runner)
+ @runner = test_runner(TestMessageHandler)
+ @handler = @runner.handler
end
subject{ @handler }
- should have_imeths :init, :init!, :run, :run!
+ should have_imeths :qs_init, :init!, :qs_run, :run!
+ should have_imeths :qs_run_callback
should "know its params and logger" do
- assert_equal @runner.params, subject.public_params
assert_equal @runner.logger, subject.public_logger
+ assert_equal @runner.params, subject.public_params
end
- should "call `init!` and its before/after init callbacks using `init`" do
- subject.init
+ should "have called `init!` and its before/after init callbacks" do
assert_equal 1, subject.first_before_init_call_order
assert_equal 2, subject.second_before_init_call_order
assert_equal 3, subject.init_call_order
assert_equal 4, subject.first_after_init_call_order
assert_equal 5, subject.second_after_init_call_order
end
- should "call `run!` and its before/after run callbacks using `run`" do
- subject.run
- assert_equal 1, subject.first_before_run_call_order
- assert_equal 2, subject.second_before_run_call_order
- assert_equal 3, subject.run_call_order
- assert_equal 4, subject.first_after_run_call_order
- assert_equal 5, subject.second_after_run_call_order
+ should "not have called `run!` and its before/after run callbacks" do
+ assert_nil subject.first_before_run_call_order
+ assert_nil subject.second_before_run_call_order
+ assert_nil subject.run_call_order
+ assert_nil subject.first_after_run_call_order
+ assert_nil subject.second_after_run_call_order
end
- should "raise a not implemented error when `run!` by default" do
- assert_raises(NotImplementedError){ @handler_class.new(@runner).run! }
+ should "run its callbacks with `qs_run_callback`" do
+ subject.qs_run_callback 'before_run'
+ assert_equal 6, subject.first_before_run_call_order
+ assert_equal 7, subject.second_before_run_call_order
end
+ should "know if it is equal to another message handler" do
+ handler = TestMessageHandler.new(@runner)
+ assert_equal handler, subject
+
+ handler = Class.new{ include Qs::MessageHandler }.new(Factory.string)
+ assert_not_equal handler, subject
+ end
+
end
+ class RunTests < InitTests
+ desc "and run"
+ setup do
+ @handler.qs_run
+ end
+
+ should "call `run!` and it's callbacks" do
+ assert_equal 6, subject.first_before_run_call_order
+ assert_equal 7, subject.second_before_run_call_order
+ assert_equal 8, subject.run_call_order
+ assert_equal 9, subject.first_after_run_call_order
+ assert_equal 10, subject.second_after_run_call_order
+ end
+
+ end
+
+ class PrivateHelpersTests < InitTests
+ setup do
+ @something = Factory.string
+ end
+
+ should "call to the runner for its logger" do
+ stub_runner_with_something_for(:logger)
+ assert_equal @runner.logger, subject.instance_eval{ logger }
+ end
+
+ should "call to the runner for its params" do
+ stub_runner_with_something_for(:params)
+ assert_equal @runner.params, subject.instance_eval{ params }
+ end
+
+ private
+
+ def stub_runner_with_something_for(meth)
+ Assert.stub(@runner, meth){ @something }
+ end
+
+ end
+
class TestMessageHandler
include Qs::MessageHandler
attr_reader :first_before_init_call_order, :second_before_init_call_order
attr_reader :first_after_init_call_order, :second_after_init_call_order
@@ -218,18 +279,9 @@
private
def next_call_order
@order ||= 0
@order += 1
- end
- end
-
- class FakeRunner
- attr_accessor :params, :logger
-
- def initialize
- @params = Factory.string
- @logger = Factory.string
end
end
end