require 'assert' require 'mr/after_commit' require 'much-plugin' require 'mr/after_commit/fake_record' require 'mr/after_commit/record_procs_methods' require 'mr/model' module MR::AfterCommit class UnitTests < Assert::Context desc "MR::AfterCommit" subject{ MR::AfterCommit } should "use much-plugin" do assert_includes MuchPlugin, subject end should "know its valid callback types" do exp = RecordProcsMethods::VALID_CALLBACK_TYPES assert_equal exp, VALID_CALLBACK_TYPES end should "know its default callback type" do exp = RecordProcsMethods::DEFAULT_CALLBACK_TYPE assert_equal exp, DEFAULT_CALLBACK_TYPE end end class MixinTests < UnitTests desc "when mixed in" setup do @model_class = Class.new do include MR::AfterCommit end end subject{ @model_class } should "be an MR model" do assert_includes MR::Model, subject end end class InitTests < MixinTests desc "and init" setup do @fake_record_class = Class.new do include MR::AfterCommit::FakeRecord end @fake_record = @fake_record_class.new @model = @model_class.new(@fake_record) end subject{ @model } should have_imeths :after_commit_procs, :after_commit, :prepend_after_commit should have_imeths :clear_after_commit_procs, :called_after_commit_procs should "demeter it's record" do callback_type, callback_proc = add_after_commit_proc assert_includes callback_proc, subject.after_commit_procs(callback_type) exp = @fake_record.after_commit_procs(callback_type) assert_equal exp, subject.after_commit_procs(callback_type) subject.clear_after_commit_procs(callback_type) assert_equal [], subject.after_commit_procs(callback_type) callback_procs = Factory.integer(3).times.map do callback_type, callback_proc = add_after_commit_proc callback_proc end callback_procs.each do |callback_proc| assert_includes callback_proc, subject.after_commit_procs end assert_equal @fake_record.after_commit_procs, subject.after_commit_procs subject.clear_after_commit_procs assert_equal [], subject.after_commit_procs callback_procs = Factory.integer(3).times.map do callback_type, callback_proc = add_after_commit_proc callback_proc end assert_equal [], subject.called_after_commit_procs subject.save # create subject.save # update subject.destroy callback_procs.each do |callback_proc| assert_includes callback_proc, subject.called_after_commit_procs end end private def add_after_commit_proc callback_type = VALID_CALLBACK_TYPES.sample callback_proc = proc{ Factory.string } if Factory.boolean subject.after_commit(callback_type, &callback_proc) else subject.prepend_after_commit(callback_type, &callback_proc) end [callback_type, callback_proc] end end end