2010-10-19 28 views
2

我想寫一個rspec測試來測試控制器是否使用了正確的佈局。 (其實我想測試沒有使用佈局:))。如何測試在使用RSpec和Rails 3的控制器中是否使用了正確的佈局

我做了一些谷歌搜索,並在這裏Testing rendering of a given layout with RSpec & Rails

也看了,但所有這一切都不會爲Rails3中工作。

我用:

controller.layout

controller.class.read_inheritable_attribute(:layout)

但這些都不給我用實​​際佈局。

你有什麼想法如何得到哪個佈局用於控制器?

回答

2

嘗試足夠response.layout

編輯

當然,response.layout不再Rspec2工作。然而,你可以驗證使用render_template作爲本Rails Forum thread描述正確的佈局被渲染:

response.should render_template("layouts/mylayout") 

關於你問題的第二部分,我沒有看到檢查佈局的沒有的方式。 response.should_not render_template("layouts/mylayout")似乎不起作用。見this discussion

+1

沒有出現在RoR3'layout'爲響應對象沒有這樣的方法: NoMethodError(#爲未定義的方法'佈局」 ): – oskarae 2010-10-21 12:21:47

+0

你說得對。看到我編輯的答案。 – zetetic 2010-10-23 21:05:10

相關問題