Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSpec matchers #147

Closed
fabn opened this issue Jul 4, 2014 · 12 comments
Closed

RSpec matchers #147

fabn opened this issue Jul 4, 2014 · 12 comments
Assignees

Comments

@fabn
Copy link

fabn commented Jul 4, 2014

Are you interested in bundling some RSpec matchers in your gem?

Currently I'm using a simple matcher which only allows to check for permissible states

describe Order do
  subject { FactoryGirl.create(:ready_order) }
  it { should be_allowed_to_go_in :created, :sent }
  it { should_not be_allowed_to_go_in :delivered }
end

Here's the matcher:

  # AASM state matcher
  matcher :be_allowed_to_go_in do |*new_states|

    match do |actual|
      @exclusively ?
          new_states.to_set.subset?(permitted_states.to_set) :
          new_states.to_set.difference(permitted_states).empty?
    end

    match_when_negated do |actual|
      (new_states & permitted_states).empty?
    end

    failure_message do
      "expected #{actual.inspect} to be allowed to go in all of #{new_states}" <<
          "#{' exclusively' if @exclusively}, permissible states was #{permitted_states}"
    end

    failure_message_when_negated do
      "expected #{actual.inspect} not to be allowed to go in any of #{new_states}, permissible states was #{permitted_states}"
    end

    chain :exclusively do
      @exclusively = true
      self
    end

    def permitted_states
      @permitted_states ||= actual.aasm.states(permitted: true).map(&:name)
    end

  end

  # AASM simple event matcher
  # @example
  #   it { should allow_event :compose }
  matcher :allow_event do |event|

    match do |actual|
      actual.send("may_#{event}?")
    end

  end
@alto alto self-assigned this Aug 15, 2014
@alto alto added this to the aasm_4.1 milestone Nov 16, 2014
@supertinou
Copy link

👍

1 similar comment
@McRip
Copy link

McRip commented Aug 25, 2015

👍

@alto
Copy link
Member

alto commented Sep 11, 2015

Now I have time for new features again. Having rspec matchers or minitest assertions available for AASM would be a great feature.

Should that be part of a core AASM or more an extra extension gem (e.g. aasm_rspec and aasm_minitest). What do you think?

@alto alto modified the milestones: 4.4, 4.3 Sep 11, 2015
@McRip
Copy link

McRip commented Sep 11, 2015

In my opinion it should be a core feature. All the other gems i know that provide rspec matchers include these in their core.

@alto
Copy link
Member

alto commented Sep 11, 2015

On the other side, it's increasing the gem size and is not really a core feature of a state machine. In my opinion it's an add-on.

Any other opinions out there?

@fabn
Copy link
Author

fabn commented Sep 11, 2015

@alto it would increase gem disk usage (by a few kilobytes) but not the memory usage because they won't be required by library code. They will need to be manually required by user in spec_helper file.

Lot of gems already use this approach, see for instance carrierwave, they provide a single file to be required if used.

Having a separate gem only to provide a couple of files would be pointless in my opinion.

@fabn
Copy link
Author

fabn commented Sep 11, 2015

I've updated the matcher above with the version I'm currently using.

@alto
Copy link
Member

alto commented Oct 27, 2015

Would these matchers be a start?

job = Job.new # single state machine per class
job.should have_state :sleeping
job.should allow_transition_to :running
job.should allow_event :run
job.should allow_event :run!
job.should_not allow_event :clean
job.should_not allow_event :clean!

# and
simple = SimpleMultipleExample.new # multiple state machines per class
simple.should have_state :standing, :on => :move
simple.should allow_transition_to :walking, :on => :move
simple.should allow_event :run
simple.should allow_event :run!
simple.should have_state :sleeping, :on => :work
simple.should allow_transition_to :processing, :on => :work
simple.should allow_event :start
simple.should allow_event :start!
simple.should_not allow_event :stop
simple.should_not allow_event :stop!

@kiela
Copy link

kiela commented Oct 27, 2015

@alto Looks good to me. Do you have already matchers for that or are you planning to write them?

@alto
Copy link
Member

alto commented Oct 27, 2015

@kiela I'm planning to write them, based on the code snippets in this ticket. Do you want to help?

@alto alto removed this from the 4.4 milestone Oct 27, 2015
@alto
Copy link
Member

alto commented Oct 30, 2015

Will be released with #279 . Feel free to have a look and try it out.

@alto alto closed this as completed Oct 30, 2015
@alto
Copy link
Member

alto commented Nov 16, 2015

Released version 4.5.0 including the new RSpec matchers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants