Class: ActiveAttr::Matchers::HaveAttributeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_attr/matchers/have_attribute_matcher.rb

Overview

Verifies that an ActiveAttr-based model has an attribute matching the given criteria. See #have_attribute

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

#of_type(type) ⇒ HaveAttributeMatcher

Specify that the attribute should have the given type

Examples:

Person’s first name should be a String

describe Person do
  it { should have_attribute(:first_name).of_type(String) }
end

Parameters:

  • type (Class)

    The expected type

Returns:

Since:

  • 0.5.0



53
54
55
56
57
58
59
# File 'lib/active_attr/matchers/have_attribute_matcher.rb', line 53

def of_type(type)
  @attribute_options[:type] = type
  @description << " of type #{type}"
  @expected_ancestors << "ActiveAttr::TypecastedAttributes"
  @attribute_expectations << lambda { @model_class._attribute_type(attribute_name) == type }
  self
end

#with_default_value_of(default_value) ⇒ HaveAttributeMatcher

Specify that the attribute should have the given default value

Examples:

Person’s first name should default to John

describe Person do
  it do
    should have_attribute(:first_name).with_default_value_of("John")
  end
end

Parameters:

  • default_value (Object)

    The expected default value

Returns:

Since:

  • 0.5.0



75
76
77
78
79
80
81
# File 'lib/active_attr/matchers/have_attribute_matcher.rb', line 75

def with_default_value_of(default_value)
  @attribute_options[:default] = default_value
  @description << " with a default value of #{default_value.inspect}"
  @expected_ancestors << "ActiveAttr::AttributeDefaults"
  @attribute_expectations << lambda { @model_class.allocate.send(:_attribute_default, @attribute_name) == default_value }
  self
end