require "spec_helper" module Hexx describe Service do around :each do |example| class Test < Service attr_reader :on_something, :something private :on_something, :something end example.run Hexx.send :remove_const, :Test end let!(:described_class) { Test } it "includes Whisper::Publisher" do expect(subject).to be_kind_of Wisper::Publisher end it "includes helpers from Parameters" do expect(described_class).to include Hexx::Helpers::Parameters expect(described_class).to have_private_method :allow_params expect(subject).to have_private_method :params end it "includes helpers from Messages" do expect(described_class).to include Hexx::Helpers::Messages expect(subject).to have_public_method :messages expect(subject).to have_private_method :add_message expect(subject).to have_private_method :t end it "includes helpers from Validations" do expect(described_class).to include Hexx::Helpers::Validations expect(subject).to have_private_method :validate! end it "includes helpers from Exceptions" do expect(described_class).to include Hexx::Helpers::Exceptions expect(described_class).to have_private_method :raises expect(subject).to have_private_method :on_error expect(subject).to have_private_method :escape end it "doesn't extend Hexx::Dependable by default" do expect(described_class).not_to be_kind_of Hexx::Dependable end describe ".new" do before { described_class.send :allow_params, :name } subject { described_class.new(name: "name", code: "code") } it "assigns params properly" do expect(subject.send :params).to eq("name" => "name") end end describe "#run" do it "is defined" do expect(subject).to respond_to :run end end describe "##run_service" do def service_spy(result) object = described_class.new allow(object).to receive(:run) { object.send :publish, result } class_spy described_class.name, new: object end let(:other_service_class) { service_spy :result } before { allow(subject).to receive(:on_service_result) } it "if wrong class given it fails with TypeError" do expect { subject.send :run_service, String, :on_string } .to raise_error { TypeError } end it "receives notifications from a service object" do subject.send :run_service, other_service_class, :on_service expect(subject).to have_received(:on_service_result) end end end end