Testing-Rendering eines bestimmten Layout mit RSpec & Rails
-
01-07-2019 - |
Frage
Ist es möglich, die Verwendung eines bestimmten Layout mit RSpec mit Rails, zum Beispiel zu testen, würde ich einen Matcher möchte, die die folgenden:
response.should use_layout('my_layout_name')
fand ich eine use_layout Matcher wenn googeln, aber es funktioniert nicht, da weder die Antwort oder Controller scheinen eine Layout-Eigenschaft zu haben, die für Matcher hinsah.
Lösung
fand ich ein Beispiel für wie ein use_layout
Matcher schreiben , die genau das tun wird. Hier ist der Code für den Fall, dass Link weggeht:
# in spec_helper.rb
class UseLayout
def initialize(expected)
@expected = 'layouts/' + expected
end
def matches?(controller)
@actual = controller.layout
#@actual.equal?(@expected)
@actual == @expected
end
def failure_message
return "use_layout expected #{@expected.inspect}, got #
{@actual.inspect}", @expected, @actual
end
def negeative_failure_message
return "use_layout expected #{@expected.inspect} not to equal #
{@actual.inspect}", @expected, @actual
end
end
def use_layout(expected)
UseLayout.new(expected)
end
# in controller spec
response.should use_layout("application")
Andere Tipps
David Chelimsky posted eine gute Antwort über auf dem Ruby-Forum :
response.should render_template("layouts/some_layout")
Dies funktioniert für mich mit Kanten Rails und Kanten RSpec on Rails:
response.layout.should == 'layouts/application'
Sollte nicht schwer sein, das für Sie geeignet in einen Matcher zu drehen.
Es gibt bereits eine perfekt funktionierende Matcher für diese:
response.should render_template(:layout => 'fooo')
(Rspec 2.6.4)
Ich hatte folgendes zu machen, diese Arbeit zu schreiben:
response.should render_template("layouts/some_folder/some_layout", "template-name")
Hier ist eine aktualisierte Version des Matcher. Ich habe es aktualisiert auf die neueste Version von RSpec anzupassen. Ich habe die entsprechende Lese hinzugefügt nur Attribute und altes Rückgabeformat entfernen.
# in spec_helper.rb
class UseLayout
attr_reader :expected
attr_reader :actual
def initialize(expected)
@expected = 'layouts/' + expected
end
def matches?(controller)
if controller.is_a?(ActionController::Base)
@actual = 'layouts/' + controller.class.read_inheritable_attribute(:layout)
else
@actual = controller.layout
end
@actual ||= "layouts/application"
@actual == @expected
end
def description
"Determines if a controller uses a layout"
end
def failure_message
return "use_layout expected #{@expected.inspect}, got #{@actual.inspect}"
end
def negeative_failure_message
return "use_layout expected #{@expected.inspect} not to equal #{@actual.inspect}"
end
end
def use_layout(expected)
UseLayout.new(expected)
end
Zusätzlich wird die Matcher funktionieren nun auch mit Layout an der Controller-Klasse-Ebene festgelegt und können wie folgt verwendet werden:
class PostsController < ApplicationController
layout "posts"
end
Und in der Controller-Spezifikation können Sie einfach:
it { should use_layout("posts") }
Hier ist die Lösung, die ich mit dem Gehen landete. Sein für rpsec 2 und Schienen 3.
Habe ich nur noch diese Datei im spec / Support-Verzeichnis.
Der Link lautet: https://gist.github.com/971342
# spec/support/matchers/render_layout.rb
ActionView::Base.class_eval do unless instance_methods.include?('_render_layout_with_tracking') def _render_layout_with_tracking(layout, locals, &block) controller.instance_variable_set(:@_rendered_layout, layout) _render_layout_without_tracking(layout, locals, &block) end alias_method_chain :_render_layout, :tracking end end
# You can use this matcher anywhere that you have access to the controller instance, # like in controller or integration specs. # # == Example Usage # # Expects no layout to be rendered: # controller.should_not render_layout # Expects any layout to be rendered: # controller.should render_layout # Expects app/views/layouts/application.html.erb to be rendered: # controller.should render_layout('application') # Expects app/views/layouts/application.html.erb not to be rendered: # controller.should_not render_layout('application') # Expects app/views/layouts/mobile/application.html.erb to be rendered: # controller.should_not render_layout('mobile/application') RSpec::Matchers.define :render_layout do |*args| expected = args.first match do |c| actual = get_layout(c) if expected.nil? !actual.nil? # actual must be nil for the test to pass. Usage: should_not render_layout elsif actual actual == expected.to_s else false end end
failure_message_for_should do |c| actual = get_layout(c) if actual.nil? && expected.nil? "expected a layout to be rendered but none was" elsif actual.nil? "expected layout #{expected.inspect} but no layout was rendered" else "expected layout #{expected.inspect} but #{actual.inspect} was rendered" end end
failure_message_for_should_not do |c| actual = get_layout(c) if expected.nil? "expected no layout but #{actual.inspect} was rendered" else "expected #{expected.inspect} not to be rendered but it was" end end
def get_layout(controller) if template = controller.instance_variable_get(:@_rendered_layout) template.virtual_path.sub(/layouts\//, '') end end end
response.should render_template("layouts/some_folder/some_layout")
response.should render_template("template-name")
controller.active_layout.name
funktioniert für mich.
Hier ist eine Version von dmcnally des Code, der keine Argumente erlaubt weitergegeben werden, so dass „sollte use_layout“ und „should_not use_layout“ Arbeit (behaupten, dass die Steuerung jedes Layout verwendet, oder kein Layout, bzw. - von denen ich würde erwarten, nur die zweite, nützlich sein, wie Sie das konkretisieren sollten, wenn es mit einem Layout ist):
class UseLayout
def initialize(expected = nil)
if expected.nil?
@expected = nil
else
@expected = 'layouts/' + expected
end
end
def matches?(controller)
@actual = controller.layout
#@actual.equal?(@expected)
if @expected.nil?
@actual
else
@actual == @expected
end
end
def failure_message
if @expected.nil?
return 'use_layout expected a layout to be used, but none was', 'any', @actual
else
return "use_layout expected #{@expected.inspect}, got #{@actual.inspect}", @expected, @actual
end
end
def negative_failure_message
if @expected.nil?
return "use_layout expected no layout to be used, but #{@actual.inspect} found", 'any', @actual
else
return "use_layout expected #{@expected.inspect} not to equal #{@actual.inspect}", @expected, @actual
end
end
end
def use_layout(expected = nil)
UseLayout.new(expected)
end
Shoulda Matchers ein Matcher für dieses Szenario. ( Dokumentation ) Dies scheint zu funktionieren:
expect(response).to render_with_layout('my_layout')
es erzeugt entsprechende Fehlermeldungen wie:
Erwartete mit dem "calendar_layout" Layout zu machen, aber mit "Anwendung", "Anwendung"
gerendert Getestet mit rails 4.2
, rspec 3.3
und shoulda-matchers 2.8.0
Edit: shoulda-Matcher diese Methode zur Verfügung stellt. Shoulda :: Matchers :: Action :: RenderWithLayoutMatcher