Testing mobile views for a rails app
In a rails app, if I use a different request format for mobiles. e.g. render *.mob开发者_如何学Goile.erb. What are the best practices to write tests for this? I use RSpec and can't find information on how to force it to render mobile views.
I'm assuming you mean view specs (as opposed to integration tests)? RSpec2's render
method delegates to ActionView::Base#render
so you can pass :file, :template, :format, etc to render
in your view specs. A better approach would probably be to just specify the correct view when describing the spec like so (example ripped form RSpec2's website):
describe "events/index.mobile.erb" do
it "renders _event partial for each event" do
assign(:events, [stub_model(Event), stub_model(Event)])
render
view.should render_template(:partial => "_event", :count => 2)
end
end
For integration tests you will need to set the user-agent. Here is a link to another question that addresses changing the user-agent (it's the same for RSpec): Is it possible to specify a user agent in a rails integration test or spec?
Best of luck!
I don't know the exact answer but just change the user-agent of the request's headers to a mobile's User-agent
精彩评论