0
根據代碼下面的代碼(taken from here):測試什麼看法呈現
describe WidgetsController do
describe "index" do
it "renders the index template" do
get :index
expect(response).to render_template("index")
expect(response.body).to eq ""
end
it "renders the widgets/index template" do
get :index
expect(response).to render_template("widgets/index")
expect(response.body).to eq ""
end
end
end
好像我們可以測試某一個觀點被渲染,而無需實際渲染視圖本身,這是一個很好的單位規範。
但是,它並沒有爲我工作:
subject { response }
describe '#create' do
context 'with valid params' do
before do
post :create, asset: { uploaded_file: file }
end
it { should have_http_status 302 }
it { should redirect_to '/' }
it { should render_template 'home' }
end
end
結果:
AssetsController
#create
with valid params
should respond with numeric status code 302
should redirect to "/"
should render template matcher "home" (FAILED - 1)
Failures:
1) AssetsController#create with valid params should render template matcher "home"
Failure/Error: it { should render_template 'home' }
expecting <"home"> but rendering with <[]>
# ./spec/controllers/assets_controller.rb:27:in `block (4 levels) in <top (required)>'
所以,我應該怎麼去測試的看法,並沒有使他們?
好的,我明白了。在重定向之後,測試哪些視圖最終會切斷到用戶,然後將路由文件和其他操作拉入單元規範:) – Starkers
您是否會說控制器規範僅測試狀態代碼,重定向到哪個/呈現以及哪些實例變量設置? – Starkers
是的,差不多。你可以選擇使它實際上做渲染,但不那麼常見 –