Sha256: 5f57abea2b1043e02162e18229e62b20ae343e1b28b5bd9e173bb26d3926f4b8

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require_relative "spec_helper"

describe Object, ".attr_implement" do
  it "creates 0-argument methods that raise" do
    klass = Class.new do
      attr_implement :foo, :bar
    end

    example = klass.new
    exception = lambda { example.foo }.must_raise RuntimeError
    exception.message.must_equal "Implement a 'foo()' method"
  end

  it "allows specifying arity and argument names" do
    klass = Class.new do
      attr_implement :foo, [:name, :age]
    end

    example = klass.new

    exception = lambda { example.foo(1, 2) }.must_raise RuntimeError
    exception.message.must_equal "Implement a 'foo(name, age)' method"

    lambda { example.foo }.must_raise ArgumentError
  end

  it "does not raise if method is implemented in a subclass" do
    klass = Class.new do
      attr_implement :foo
    end

    subklass = Class.new(klass) do
      def foo
        "bar"
      end
    end

    subklass.new.foo.must_equal "bar"
  end

  # E.g. Active Record defines column query methods like "admin?"
  # higher up in the ancestor chain.
  it "does not raise if method is implemented in a superclass" do
    superklass = Class.new do
      def foo
        "bar"
      end
    end

    klass = Class.new(superklass) do
      attr_implement :foo
    end

    klass.new.foo.must_equal "bar"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attr_extras-3.2.0 spec/attr_implement_spec.rb