2010-12-12 29 views
12

Làm cách nào để kiểm tra bố cục cụ thể được sử dụng trong RSpec? Tôi đã thử template.layout, response.layout và response.should render_template ("layout") không có may mắn.Bố cục thử nghiệm Rspec

Trả lời

17

Trong rspec 2, trong thông số bộ điều khiển, bạn sử dụng render_template như bạn đoán, nhưng bạn cần bao gồm đường dẫn tương ứng với thư mục lượt xem. Vì vậy, nếu bố trí của bạn là app/views/layouts/mylayout.html.erb, spec của bạn trông như thế này:

response.should render_template "layouts/mylayout" 
+0

Và làm cách nào để bạn kiểm tra không có bố cục nào được sử dụng. Giả sử rằng bộ điều khiển thực hiện 'render: layout => false' chẳng hạn. Làm thế nào để bạn kiểm tra nó? –

1
# 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 
+1

https://gist.github.com/11080d61648aaee51840 Tôi đang ở trên rspec 2, đường ray 3. Tôi bị phản đối với response.layout và các lỗi mẫu phát.được đề xuất. –

+0

Tôi đã cập nhật câu trả lời của mình dựa trên phản hồi của bạn. –

5

Ngoài ra, bạn có thể kiểm tra cả hai, cách bố trí và rendering hành động, trong một lớp lót trong rspec-2:

response.should render_template(%w(layouts/application name_of_controller/edit)) 
5

được cập nhật cú pháp cho RSpec 3:

expect(response).to render_template(:index) # view 
expect(response).to render_template(layout: :application) # layout 

RSpec docs

Hoặc nếu bạn thích @Flov's one-liner, bạn có thể viết:

expect(response).to render_template(:index, layout: :application) 

Lưu ý rằng render_template đại biểu assert_template. Bạn có thể tìm thấy các tài liệu đó tại đây: ActionController assert_template.

+0

Chỉ cần một lưu ý, điều này không còn hoạt động và cũng là tài liệu được man rợ lỗi thời. – Sebastialonso

Các vấn đề liên quan