Sha256: a732222f5aebf187e74a52361d70edbcedca1b7abd3c223502ccbe56ddb25562

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

require "spec_helper"

describe AdaptivePayments::Node do
  let(:child_model) do
    Class.new(AdaptivePayments::JsonModel) do
      attribute :example, String
    end
  end

  let(:model) do
    klass = child_model
    Class.new(AdaptivePayments::JsonModel) do
      attribute :child, AdaptivePayments::Node[klass]
    end
  end

  describe "default type" do
    let(:child) { model.new.child }

    it "is an instance of the boxed type" do
      child.should be_an_instance_of(child_model)
    end
  end

  describe "coercion" do
    let(:object) { model.new(:child => { :example => "anything" }) }

    it "coerces hash to instances of the given type" do
      object.child.should be_an_instance_of(child_model)
    end

    it "maintains the original keys" do
      object.child.example.should == "anything"
    end
  end

  describe "#to_json" do
    context "when not empty" do
      let(:json) { model.new(:child => { :example => "whatever" }).to_json }

      it "is present as a child in the output" do
        json.should == '{"child":{"example":"whatever"}}'
      end
    end

    context "when empty" do
      let(:json) { model.new.to_json }

      it "is omitted from the output" do
        json.should == '{}'
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pp-adaptive-1.0.0 spec/public/node_spec.rb
pp-adaptive-0.0.6 spec/public/node_spec.rb
pp-adaptive-0.0.5 spec/public/node_spec.rb
pp-adaptive-0.0.4 spec/public/node_spec.rb
pp-adaptive-0.0.3 spec/public/node_spec.rb
pp-adaptive-0.0.2 spec/public/node_spec.rb