hzamani / active_record-acts_as

Simulate multi-table inheritance for activerecord models
MIT License
252 stars 86 forks source link

Custom matchers for RSpec? #51

Closed tiagoamaro closed 8 years ago

tiagoamaro commented 8 years ago

Hey @hzamani,

first of all, thank you very much for this gem. It already helped me a lot on past projects, and it's helping me again :)

On a current project, I noticed that I wanted to do a quick check on classes and instances of those classes to see if they were acting as/actables of a certain class or symbol, so I dig up your specs and saw that you have some nice boolean methods (#acting_as? + .acting_as? + .actable?) to verify that. Do you think it's interesting adding some custom matchers to the library to help people out writing specs?

Example:

# Sample rails_helper.rb

require 'rspec/acts_as_matchers'

# acts_as_matchers.rb

RSpec::Matchers.define :acts_as do |actable|
  match { |actor| actor.acting_as?(actable) }
end

RSpec::Matchers.define :be_actable do
  match { |instance| instance.class.actable? }
end

And then on specs, people would have available the following interfaces:

it { is_expected.to acts_as(:account) }
it { expect(Person).to acts_as(:account) }

it { is_expected.to acts_as(Account) }
it { expect(Person).to acts_as(Account) }

it { expect(described_class).to be_actable }

What do you think? :)

If you like the idea, I can open a PR with those changes right away. If not, I'll just close the issue.

hzamani commented 8 years ago

Thanks @tiagoamaro it's always nice to have test helpers :+1: