我正在尝试为组合静态内容路由编写一个测试。我是通过直接检查环响应来测试路线的。
一个最低限度的工作示例如下:
;; src/testing-webapps.core.clj
(ns testing-webapps.core
(:use [compojure.core]
[compojure.route :as route]))
(defroutes web-app
(route/resources "/")
(route/not-found "404"))
;; test/testing-webapps.core_test.clj
(ns testing-webapps.core-test
(:require [clojure.test :refer :all]
[testing-webapps.core :refer :all]))
(defn request [resource web-app & params]
(web-app {:request-method :get :uri resource :params (first params)}))
(deftest test-routes
(is (= 404 (:status (request "/flubber" web-app))))
(is (= "404" (:body (request "/flubber" web-app))))
(is (= 200 (:status (request "/test.txt" web-app)))))测试404路由运行良好,但是调用(request "/test.txt" web-app)会导致ring.middleware.file-info/not-modified-since?中的意外NullPointerException。
下面是堆栈跟踪的顶部部分:
ERROR in (test-routes) (file_info.clj:27)
Uncaught exception, not in assertion.
expected: nil
actual: java.lang.NullPointerException: null
at ring.middleware.file_info$not_modified_since_QMARK_.invoke (file_info.clj:27)
ring.middleware.file_info$file_info_response.doInvoke (file_info.clj:44)
clojure.lang.RestFn.invoke (RestFn.java:442)
ring.middleware.file_info$wrap_file_info$fn__917.invoke (file_info.clj:64)
[...]静态路由在浏览器中运行良好,但在通过我的request函数调用时却不能工作。
是否有更简单的方法来测试静态路由?为什么我在使用自己的请求映射调用静态路由时获得一个NullPointerException?
发布于 2013-12-04 15:00:21
查看not-modified-since?的源代码,我认为问题在于您的请求映射中没有标头,因此它在这个expr:(headers "if-modified-since")上抛出了一个NPE。尝试更改您的request方法如下:
(defn request [resource web-app & params]
(web-app {:request-method :get
:headers {"content-type" "text/plain"} ; added a header
:uri resource
:params (first params)}))您还可以考虑使用ring-mock创建测试请求。它会让你有点远离这种东西。
https://stackoverflow.com/questions/20377132
复制相似问题