1
我试图写一些快速路由测试一个简单的API,所以我写道:如何使用rspec来测试正在救援和重定向的路线?
it "delete" do
delete("/api/notifications/:id").should_not be_routable
end
但我收到:
Failure/Error: delete("/api/notifications/:id").should_not be_routable
expected {:delete=>"/api/notifications/:id"} not to be routable, but it routes to {:controller=>"application", :action=>"rescue404", :a=>"api/notifications/:id"}
我很快就意识到我是从404抢救,所以几乎一切是可路由的。
unless Rails.env.development?
rescue_from NotFound, :with => :rescue404
rescue_from ActiveRecord::RecordNotFound, :with => :rescue404
rescue_from ActionController::RoutingError, :with => :rescue404
end
def rescue404
respond_to do |format|
format.json { render :text => 'Something went wrong. Record not found or url is incorrect.\n' }
format.xml { render :text => 'Something went wrong. Record not found or url is incorrect.\n' }
format.html { redirect_to root_url, :alert => 'That page does not exist, sorry bro!' }
end
end
这给我留下了这样一个测试:
it "delete" do
delete("/api/notifications/:id").should route_to("application#rescue404", :a => "api/notifications/:id")
end
写这样很容易出错,我为我不断得到“:A =>”错误的。有什么方法可以测试一个异常是否被救出?
这工作:
it "delete" do
delete("/api/notifications/:id").should raise_error()
end
...但什么样的错误,我应该检查?或者我应该把它留在那?