문제

is there any good way to test a controller with rspec? Actually, I'm testing the complete output (including the rendered view) with rack/test.

Take the following minimal controller for example:

MyApp.controllers :cool_controller do

  get :index do
    some_var = DateTime.now
    render 'some_view', :locals => { :dont_know => nil, :some_var => some_var }
  end

end

What would be the best option to test, that the values in the locals hash are like I expect them to be?

도움이 되었습니까?

해결책

For this case, it seems you want to test: a) DateTime.now gives you a valid value - test that directly using classical rspec. b) the var some_var is passed to render, but that is trivial.

It sounds like you might actually want to test the view in isolation. Somthing like this (untested):

it 'shows the time now' do
    right_now = DateTime.now
    engine = Haml::Engine.new(IO.read('some_view.haml'))
    rendered = engine.render(Object.new, :@some_var => right_now)
    rendered.should have_css('ul#some_var li', :text => right_now)
end 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top