Sha256: c168e6d0bcf6eb0d1a9a88da1f65e294fc4e684fae4c878e1fbc1f1aa5c06561

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

require "spec_helper"

describe AdaptivePayments::NodeList 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 :children, AdaptivePayments::NodeList[klass]
    end
  end

  describe "default type" do
    it "is a kind of Array" do
      model.new.children.should be_a_kind_of(Array)
    end
  end

  describe "coercion" do
    context "when appending to" do
      let(:object) { model.new.tap { |o| o.children << { :example => "anything" } } }

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

    context "when overwriting" do
      let(:object) { model.new.tap { |o| o.children = [{ :example => "anything" }] } }

      it "coerces each member to instances of the given type" do
        object.children.first.should be_an_instance_of(child_model)
      end
    end
  end

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

      it "is present as a child in the output" do
        json.should == '{"children":[{"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_list_spec.rb
pp-adaptive-0.0.6 spec/public/node_list_spec.rb
pp-adaptive-0.0.5 spec/public/node_list_spec.rb
pp-adaptive-0.0.4 spec/public/node_list_spec.rb
pp-adaptive-0.0.3 spec/public/node_list_spec.rb
pp-adaptive-0.0.2 spec/public/node_list_spec.rb