使用 RSpec & 测试给定布局的渲染导轨 [英] Testing rendering of a given layout with RSpec & Rails

查看:21
本文介绍了使用 RSpec & 测试给定布局的渲染导轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用带有 Rails 的 RSpec 来测试给定布局的使用,例如我想要一个执行以下操作的匹配器:

Is it possible to test the use of a given layout using RSpec with Rails, for example I'd like a matcher that does the following:

response.should use_layout('my_layout_name')

我在谷歌搜索时发现了一个 use_layout 匹配器,但它不起作用,因为响应或控制器似乎都没有匹配器正在寻找的布局属性.

I found a use_layout matcher when Googling but it doesn't work as neither the response or controller seem to have a layout property that matcher was looking for.

推荐答案

我找到了一个 如何编写一个能够做到这一点的 use_layout 匹配器.这是链接消失的代码:

I found an example of how to write a use_layout matcher that will do just that. Here's the code in case that link goes away:

# 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")

这篇关于使用 RSpec & 测试给定布局的渲染导轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆