solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Writing matcher for Boolean attribute #282

Closed jpmoral closed 9 years ago

jpmoral commented 9 years ago

We have the following code:

class MyClass
  include Virtus.model     
  attribute :my_boolean, Boolean
end

and we are trying unsuccessfully to write a matcher for it using virtus-matchers.

The following fails:

it { is_expected.to have_attribute(:my_boolean, Virtus::Attribute::Boolean) }

That project compares the passed in type (in this case Virtus::Attribute::Boolean) with the attribute's primitive. It fails apparently because the primitive of a Boolean is a BasicObject.

What's the best way of writing a matcher without depending on Virtus's private APIs?

solnic commented 9 years ago

attribute's primitive is an instance of axiom type you could use that for your matchers although I gotta warn you it's a subject to change in virtus 2.0.

In general though I wouldn't recommend writing tests like that, you're checking some internal details which is very brittle. It's way better to check actual behavior of an object rather than its attribute setup.