Sha256: 68020ec3dbaab8826fe6d1fc55c9e6a2bcfdc390c2f72e44804678453cc20ce9

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Hexx
  describe Models do

    # ==========================================================================
    # Prepare environment
    # ==========================================================================

    around do |example|
      class TestModel; include Models; end
      class TestAttribute; end
      example.run
      Hexx.send :remove_const, :TestAttribute
      Hexx.send :remove_const, :TestModel
    end

    let!(:coerced_attribute) { double "coerced attribute" }
    before do
      allow(TestAttribute).to receive(:new) { |val| coerced_attribute if val }
    end

    # ==========================================================================
    # Prepare variables
    # ==========================================================================

    let!(:subject) { TestModel }

    # ==========================================================================
    # Run tests
    # ==========================================================================

    describe "#validate!" do

      let!(:object)  { subject.new }

      it "is public" do
        expect(object).to respond_to(:validate!).with(0).arguments
      end

      it "returns true for valid instance" do
        allow(object).to receive(:valid?) { true }
        expect(object.validate!).to be_truthy
      end

      it "fails with Hexx::RecordInvalid for invalid instance" do
        allow(object).to receive(:valid?) { false }
        expect { object.validate! }.to raise_error do |error|
          expect(error).to be_kind_of Hexx::RecordInvalid
          expect(error.object).to eq object
        end
      end
    end

    describe ".attr_coerced" do

      it "coerces an attribute with given type" do
        subject.send :attr_coerced, :name, type: TestAttribute
        object = subject.new
        expect { object.name = "some name" }
          .to change { object.name }
          .from(nil).to coerced_attribute
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexx-0.0.1 spec/hexx/models_spec.rb