1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/render_template_matcher.rb
Kapil Sachdev 3e88500318 fix(rubocop): Fix Style, Layout, Lint and other offenses [ci skip]
Updated Layout/LineLength cop to Max 120 lenght and ignore everything 
inside specs/**/*

Added rules for below cops in .rubocop.yml
Lint/AmbiguousBlockAssociation
Naming/HeredocDelimiterNaming
Rails/SkipsModelValidations
Style/FormatStringToken


Fixed below mentioned cops:
- Layout/CaseIndentation
- Layout/DotPosition
- Layout/ElseAlignment
- Layout/IndentationWidth
- Layout/LineLength
- Layout/MultilineBlockLayout
- Layout/MultilineOperationIndentation
- Lint/AmbiguousBlockAssociation
- Lint/MissingCopEnableDirective
- Lint/NestedMethodDefinition
- Lint/RedundantCopDisableDirective
- Lint/RedundantRequireStatement
- Lint/UnusedBlockArgument
- Lint/UnusedMethodArgument
- Metrics/ModuleLength
- Naming/MemoizedInstanceVariableName
- Naming/RescuedExceptionsVariableName
- Rails/Output
- Rails/Presence
- Security/Eval
- Security/Open
- Style/ClassCheck
- Style/CollectionMethods
- Style/ConditionalAssignment
- Style/EvalWithLocation
- Style/FormatStringToken
- Style/InverseMethods
- Style/MutableConstant
- Style/ParallelAssignment
- Style/RedundantBegin
- Style/RedundantCondition
- Style/RedundantInterpolation
- Style/RedundantSelf
- Style/RedundantSort
- Style/RescueStandardError
- Style/SafeNavigation
- Style/StringLiteralsInInterpolation
- Style/SymbolProc
2020-11-03 10:05:25 -07:00

84 lines
2.4 KiB
Ruby

module Shoulda
module Matchers
module ActionController
# The `render_template` matcher tests that an action renders a template
# or partial. In RSpec, it is very similar to rspec-rails's
# `render_template` matcher. In a test suite using Minitest + Shoulda, it
# provides a more expressive syntax over `assert_template`.
#
# class PostsController < ApplicationController
# def show
# end
# end
#
# # app/views/posts/show.html.erb
# <%= render 'sidebar' %>
#
# # RSpec
# RSpec.describe PostsController, type: :controller do
# describe 'GET #show' do
# before { get :show }
#
# it { should render_template('show') }
# it { should render_template(partial: '_sidebar') }
# end
# end
#
# # Minitest (Shoulda)
# class PostsControllerTest < ActionController::TestCase
# context 'GET #show' do
# setup { get :show }
#
# should render_template('show')
# should render_template(partial: '_sidebar')
# end
# end
#
# @return [RenderTemplateMatcher]
#
def render_template(options = {}, message = nil)
RenderTemplateMatcher.new(options, message, self)
end
# @private
class RenderTemplateMatcher
attr_reader :failure_message, :failure_message_when_negated
def initialize(options, message, context)
@options = options
@message = message
@template = options.is_a?(Hash) ? options[:partial] : options
@context = context
@controller = nil
@failure_message = nil
@failure_message_when_negated = nil
end
def matches?(controller)
@controller = controller
renders_template?
end
def description
"render template #{@template}"
end
def in_context(context)
@context = context
self
end
private
def renders_template?
@context.__send__(:assert_template, @options, @message)
@failure_message_when_negated = "Didn't expect to render #{@template}"
true
rescue Shoulda::Matchers.assertion_exception_class => e
@failure_message = e.message
false
end
end
end
end
end