module SpecProducer::RspecBuilders::Builder

Public Class Methods

build(&block) click to toggle source

Call build on a RspecBuilders::Base class

Example

builder = RspecBuilders::Base.build do |b|
  # ...
  b.context('my awsome spec') do
    be.it { should_be(:valid) }
  end
end
# File lib/spec_producer/rspec_builders/builder.rb, line 36
def self.build(&block)
  instance = new
  block.call(instance)
  instance
end

Public Instance Methods

before_render() click to toggle source

Adds a before { render } block to buffer

Example

RspecBuilders::Based.build do |b|
  b.before_render
end

Produces:
  before do
     render
  end
# File lib/spec_producer/rspec_builders/builder.rb, line 125
def before_render
  add "before do"
  increase_intent
  new_line

  add "render"
  new_line

  decrease_intent
  add 'end'
  new_line
  new_line
end
build(&block) click to toggle source

Call build on an RspecBuilder::Base instance

Example

builder = RspecBuilders::Base.new('Init Text')
builder.build do |b|
  # Other method calls here
end
# File lib/spec_producer/rspec_builders/builder.rb, line 20
def build(&block)
  block.call(self)
  self
end
context(name, &block) click to toggle source

Adds a context or describe (alias) block for the spec. the method is responsible to handle the intentation management (increase / decrease).

Usage

RspecBuilders::Base.build do |b|
  b.context 'first context' do
    b.context 'first nested' do
    end
  end

  b.context 'second context' do
    b.context 'second nested' {}
  end
end

Which produces the following spec:

context 'first context' do
  context 'first nested' do
  end
end

context 'second context' do
  context 'second nested do

  end
end
# File lib/spec_producer/rspec_builders/builder.rb, line 169
def context(name, &block)
  new_line
  add "context \"#{name}\" do"
  increase_intent
  new_line

  block.call

  decrease_intent
  add 'end'
  new_line
end
Also aliased as: describe
describe(name, &block)
Alias for: context
it(*args, &block) click to toggle source

Provides an it expectation.

Usage example

user = User.new(name: 'alex', email: 'some@some.com')

RspecBuilders::Base.build do |b|
  b.subject { "build(:user, name: 'Alex', email: 'some@some.com')" }

  # Matchers provided from RspecBuilders::Matchers
  b.it { validates_presence_of(:name) }
  b.it { expect('subject.name').to eq user.name }
  b.it { expect('subject.email').to eq 'name' }

  # Or provide the expecation as a string
  b.it('expect(subject).to be_a User')
end

produces the following spec

  subject { 'builder(:user, anme: 'Alex', email: 'some@some.com')' }
  it { is_expected.to validate_presence_of(:name) }
  it { expect(subject.name).to eq 'Alex' }
  it { expect(subject.email).to eq 'some@some.com' }
  it { expect(subject).to be_a User }
# File lib/spec_producer/rspec_builders/builder.rb, line 209
def it(*args, &block)
  expectation = args.shift
  if expectation
    add "it { #{expectation} }"
    new_line
  else
    instance_eval(&block)
  end
end
pending(s) click to toggle source

Adds a pending 'some' block to buffer it also adds a new line for the next spec to be added to the new line

Example

RspecBuilders::Based.build do |b|
  b.pending { 'some missing spec' }
end

Produces:
  pending 'some missing spec'
# File lib/spec_producer/rspec_builders/builder.rb, line 107
def pending(s)
  add "pending '#{s}'"
  new_line
end
spec(klass, type, &block) click to toggle source

Creates a new spec. This is the header block required by all spec files

Example

builder = RspecBuilders::Base.build do |b|
            b.spec 'User', 'models'
          end

Calling
  puts builder

Returns the following string:

  describe User, type: :model do
  end
# File lib/spec_producer/rspec_builders/builder.rb, line 58
def spec(klass, type, &block)
  new_line

  if type == 'routing'
    add "describe '#{klass} routes', type: :#{type} do"
  elsif type == 'view'
    add "describe '#{klass}', type: :#{type} do"
  else
    add "describe #{klass}, type: :#{type} do"
  end

  increase_intent
  new_line

  block.call(self)

  decrease_intent
  add 'end'
end
subject(s) click to toggle source

Adds a subject { 'some' } block to buffer it also adds a new line for the next spec to be added to the new line

Example

RspecBuilders::Based.build do |b|
  b.subject { 'User.new' }
end

Produces:
  subject { User.new }
# File lib/spec_producer/rspec_builders/builder.rb, line 90
def subject(s)
  add "subject { #{s} }"
  new_line
end