Как проверить, что конкретный макет используется в RSpec? Я пробовал template.layout, response.layout и response.should render_template ( "layout" ) без везения.
Схема тестирования Rspec
Ответ 1
В rspec 2 в спецификации контроллера вы используете render_template, как вы уже догадались, но вам нужно указать путь относительно каталога представлений. Поэтому, если ваш макет - приложение /views/layouts/mylayout.html.erb, ваша спецификация выглядит следующим образом:
response.should render_template "layouts/mylayout"
Ответ 2
Кроме того, вы можете протестировать как макет, так и рендеринг действий в одном слое в rspec-2:
response.should render_template(%w(layouts/application name_of_controller/edit))
Ответ 3
Обновленный синтаксис RSpec 3:
expect(response).to render_template(:index) # view
expect(response).to render_template(layout: :application) # layout
Или, если вы предпочитаете @Flov с одним слоем, вы можете написать:
expect(response).to render_template(:index, layout: :application)
Обратите внимание, что render_template
делегирует assert_template
. Вы можете найти эти документы здесь: ActionController assert_template.
Ответ 4
# rspec-rails-1.3.x for rails-2
describe HomeController do
describe "the home page" do
it "should use the :home_page layout" do
get :index
response.layout.should == "layouts/home_page"
end
end
end
# rspec-2 for rails-3
describe "GET index" do
it "renders the page within the 'application' layout" do
get :index
response.should render_template 'layouts/application' # layout
response.should render_template 'index' # view
end
end