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)
RSpec 3的更新语法:
expect(response).to render_template(:index) # view
expect(response).to render_template(layout: :application) # layout
Run Code Online (Sandbox Code Playgroud)
或者如果你喜欢@ Flov的单行,你可以写:
expect(response).to render_template(:index, layout: :application)
Run Code Online (Sandbox Code Playgroud)
请注意render_template代表assert_template.你可以在这里找到这些文档:ActionController assert_template.
此外,您可以在rspec-2中以单行方式测试布局和动作渲染:
response.should render_template(%w(layouts/application name_of_controller/edit))
Run Code Online (Sandbox Code Playgroud)