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

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

问题描述

是否可以使用带有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匹配器,但是它没有用,因为响应或控制器似乎都没有匹配器正在寻找的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& amp;测试指定布局的渲染滑轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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