require "spec_helper" module Hexx class Service describe Message do describe ".new" do subject { Message } it "requires type" do expect { subject.new text: "" }.to raise_error end it "requires text" do expect { subject.new type: "" }.to raise_error end end describe ".from" do subject { Message } let(:service) { Service.new } before { service.send(:errors).add :base, :text } it "extracts error messages from a record" do message = subject.from(service).first expect(message).to be_kind_of Message expect(message.type).to eq "error" expect(message.text).to eq service.errors.values.flatten.first end end describe "#type" do subject { Message.new type: "info", text: "text" } it "returns message type" do expect(subject.type).to eq "info" end end describe "#text" do subject { Message.new type: "info", text: "text" } it "returns message text" do expect(subject.text).to eq "text" end end describe "#==" do let!(:params) { { type: "info", text: "text" } } it "returns true if type and text of two messages are the same" do expect(Message.new params).to eq(Message.new params) end it "returns false if types of two messages are different" do expect(Message.new params) .not_to eq(Message.new params.merge(type: "error")) end it "returns false if texts of two messages are different" do expect(Message.new params) .not_to eq(Message.new params.merge(text: "another")) end it "returns false when a message compares to non-message" do expect(Message.new params).not_to eq("text") end end describe "#<=>" do let!(:aa) { Message.new type: "a", text: "a" } let!(:ab) { Message.new type: "a", text: "b" } let!(:ba) { Message.new type: "b", text: "a" } it "orders messages by type" do expect(aa < ba).to be_truthy end it "orders messages by text" do expect(aa < ab).to be_truthy end it "orders messages first by type" do expect(ab < ba).to be_truthy end it "fails when compared with non-message" do expect { ab < "" }.to raise_error end end end end end