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
...但什麼樣的錯誤,我應該檢查?或者我應該把它留在那?