Module: Resubject::Rspec

Defined in:
lib/resubject/rspec.rb

Overview

RSpec configuration and helpers

This helper automatically creates ‘subject` and `template` variables for RSpec when testing your presenter.

To get this working, create a spec file in ‘spec/presenters` and require `resubject/rspec`. You only need to define the object under test by creating a `let(:object)`, like so:

Examples:

Isolated spec for MyPresenter


# spec/presenters/my_presenter_spec.rb
require 'resubject/rspec'
require 'presenters/my_presenter'

describe MyPresenter do
  let(:object) { mock :presented }

  it 'has full name' do
    object.stub(first: 'My', last: 'Name')
    expect(subject.name).to eq 'My Name'
  end
end

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extend RSpec configuration for files placed in ‘spec/presenters`



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/resubject/rspec.rb', line 30

def self.included(base)
  base.instance_eval do
    let(:template) do
      if defined? ActionView
        ActionView::Base.new
      else
        mock :template
      end
    end

    subject do
      described_class.new(object, template)
    end
  end
end