对于确定侧边栏行为的控制器,我需要调用几个网址;侧边栏将根据其显示的页面更改(a.o)。
describe SidebarController do
before(:each) do
@sidebar = SidebarController.new
end
it 'should return :jobseekers for root_path' do
get(root_url)
@sidebar.section(root_path).should eq :jobseekers
end
end
然而,ActionController::RoutingError: No route matches {:controller=>"sidebar", :action=>"http://test.host/"}
我可以get
将网址或路径作为字符串吗?有没有更聪明的方法来设置request.
数据,而不是GETting呢?
答案 0 :(得分:17)
我可以将网址或路径作为字符串获取吗?
不在控制器测试中。测试控制器实际上并不处理URL,它使用5种支持请求类型之一(get,post,put,head,delete)设置测试,然后调用适当的控制器操作。请参阅Guide to Testing Rails Applications。
您正在寻找的是集成测试,或RSpec术语中的“request spec”。
答案 1 :(得分:7)
这是一个解决方法......如果你想这样做......不一定是最好的方法,但是一种方式..这适用于rails 3.2 ......
module ActionController::TestCase::Behavior
#just parse the url and then call the regular get method
def get_path(path)
parsed_params = Rails.application.routes.recognize_path path
controller = parsed_params.delete(:controller)
action = parsed_params.delete(:action)
get(action, parsed_params)
end
end