Rspec测试布局

Eri*_* M. 14 testing layout rspec

如何测试RSpec中是否使用了特定布局?我已经尝试过template.layout,response.layout和response.should render_template("layout")而没有运气.

Rob*_*vis 18

在rspec 2中,在控制器规范中,您在猜测时使用了render_template,但是您需要包含相对于views目录的路径.因此,如果您的布局是app/views/layouts/mylayout.html.erb,那么您的规范如下所示:

response.should render_template "layouts/mylayout"
Run Code Online (Sandbox Code Playgroud)


Luc*_*ain 7

RSpec 3的更新语法:

expect(response).to render_template(:index) # view
expect(response).to render_template(layout: :application) # layout
Run Code Online (Sandbox Code Playgroud)

RSpec文档

或者如果你喜欢@ Flov的单行,你可以写:

expect(response).to render_template(:index, layout: :application)
Run Code Online (Sandbox Code Playgroud)

请注意render_template代表assert_template.你可以在这里找到这些文档:ActionController assert_template.


Flo*_*lov 5

此外,您可以在rspec-2中以单行方式测试布局和动作渲染:

response.should render_template(%w(layouts/application name_of_controller/edit))
Run Code Online (Sandbox Code Playgroud)