Sha256: d1839bceee1c32b488bb8d731428b8abee3e869295bddfef979a8555cd249013

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

require 'spec_helper'

shared_examples_for "an attribute" do |type, default_value, another_value, *args, &block|
  options = args.extract_options!

  describe type do
    let(:name) { "a_#{type}" }
    let(:instance) { subject.new() }

    describe "as attribute" do
      before do
        subject.send :"attr_#{type}", name, *args, options
      end

      it { subject.attributes.should include(name.to_s) }
      it { instance.attributes.should include(name.to_s) }
      it { instance.should respond_to(name)}
      it { instance.should respond_to("#{name}=")}

      it "assigns attribute in initializer" do
        a = subject.new(name => another_value)
        a.send(name).should eq(another_value)
        a.attributes[name].should eq(another_value)
      end

      it "assigns attribute" do
        a = subject.new()
        a.send("#{name}=", another_value)
        a.attributes[name].should eq(another_value)
      end
    end

    describe "as required attribute" do
      before do
        subject.send :"attr_#{type}", name, *args, options.merge(:required => true)
      end

      it { instance.should validate_presence_of(name)}
    end

    describe "with default value" do
      before do
        subject.send :"attr_#{type}", name, *args, options.merge(:default => default_value)
      end

      it { instance.send(name).should eq(default_value)}
    end
  end
end

shared_examples_for "a class with attributes" do |type|

  # it's good enough to see whether the type includes
  # the class methods of the attributes module
  it { type.should be_a(Riveter::Attributes::ClassMethods) }

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
riveter-0.0.4 spec/examples/attribute_examples.rb
riveter-0.0.3 spec/examples/attribute_examples.rb
riveter-0.0.1 spec/examples/attribute_examples.rb