Module: Rails::AddOns::Shoulda::Matchers

Defined in:
lib/rails/add_ons/shoulda/matchers.rb,
lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb,
lib/rails/add_ons/shoulda/matchers/implement_index_action_matcher.rb,
lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb,
lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb,
lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb

Overview

Adding the matchers to rspec:

# spec/rails_helper or supoprt/rails-add_ons.rb
require 'rails/add_ons/shoulda/matchers'

RSpec.configure do |config|
  config.include Rails::AddOns::Shoulda::Matchers, type: :feature
end

Defined Under Namespace

Classes: ImplementCreateActionMatcher, ImplementDeleteActionMatcher, ImplementIndexActionMatcher, ImplementShowActionMatcher, ImplementUpdateActionMatcher

Instance Method Summary collapse

Instance Method Details

#implement_create_action(spec) ⇒ Object

Example:

RSpec.describe '/posts', type: :feature do
  it {
    expect(subject).to implement_create_action(self)
      .for(Post)
      .within_form('#new_post') {
        fill_in 'post[title]', with: 'My first post'
      }
      .increasing{ |resource_class| resource_class.count }.by(1)
  }
end

The newly created resource is found by calling @resource_class.last. If you need to change this you can call #finding_created_resource_with.

Example:

RSpec.describe '/posts', type: :feature do
  it {
    expect(subject).to implement_create_action(self)
      .for(resource_class)
      .within_form('#new_user') {
        # fill the needed form inputs via capybara here
        #
        # Example:
        #
        #     select 'de', from: 'slider[locale]'
        #     fill_in 'slider[name]', with: 'My first slider'
        #     check 'slider[auto_start]'
        #     fill_in 'slider[interval]', with: '3'
        fill_in 'user[email]', with: '[email protected]'
        fill_in 'user[password]', with: 'password'
        fill_in 'user[password_confirmation]', with: 'password'
      }
      .finding_created_resource_with{ Ecm::UserArea::User.order(created_at: :desc).first }
      .increasing{ Ecm::UserArea::User.count }.by(1)
  }
end

45
46
47
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 45

def implement_create_action(spec)
  ImplementCreateActionMatcher.new(spec)
end

#implement_delete_action(spec) ⇒ Object

Example:

RSpec.describe '/posts', type: :feature do
  let(:resource_class) { Post }
  let(:resource)       { create(:post) }

  it { 
   expect(subject).to implement_delete_action(self)
     .for(resource)
     .reducing{ resource_class.count }.by(1)
  }
end

18
19
20
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 18

def implement_delete_action(spec)
  ImplementDeleteActionMatcher.new(spec)
end

#implement_index_action(spec) ⇒ Object

Example:

RSpec.describe '/posts', type: :feature do
  before(:each) { create_list(:post, 3) }

  it { expect(subject).to implement_index_action(self) }
end

13
14
15
# File 'lib/rails/add_ons/shoulda/matchers/implement_index_action_matcher.rb', line 13

def implement_index_action(spec)
  ImplementIndexActionMatcher.new(spec)
end

#implement_show_action(spec) ⇒ Object

Example:

RSpec.describe '/posts', type: :feature do
  let(:resource)       { create(:post) }

   it { expect(subject).to implement_show_action(self).for(resource) }
end

13
14
15
# File 'lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb', line 13

def implement_show_action(spec)
  ImplementShowActionMatcher.new(spec)
end

#implement_update_action(spec) ⇒ Object

Example checking for changed resource attributes:

RSpec.describe '/posts', type: :feature do
  let(:post) { create(:post) }
  it {
    expect(subject).to implement_update_action(self)
      .for(post)
      .within_form('.edit_post') {
        fill_in 'post[title]', with: 'New title'
        fill_in 'post[body]',  with: 'New body'
      }
      .updating
      .from(post.attributes)
      .to({ 'title' => 'New title', 'body' => 'New body' })
  }
end

Example checking for changed state:

RSpec.describe '/posts', type: :feature do
  let(:post) { create(:post) }
  it {
    expect(subject).to implement_update_action(self)
      .for(post)
      .within_form('.edit_post') {
        fill_in 'post[title]', with: 'New title'
        fill_in 'post[body]',  with: 'New body'
      }
      .updating{ |resource| resource.updated_by }
      .from(nil)
      .to(User.current)
  }
end

39
40
41
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 39

def implement_update_action(spec)
  ImplementUpdateActionMatcher.new(spec)
end