Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 如何以编程方式要求命名空间_Clojure_Namespaces - Fatal编程技术网

Clojure 如何以编程方式要求命名空间

Clojure 如何以编程方式要求命名空间,clojure,namespaces,Clojure,Namespaces,我在Clojure做一个项目。我已经定义了一系列路由,它们返回由其他名称空间中的逻辑计算的JSON数据。我希望能够以编程方式更改实现逻辑的名称空间,以便执行以下操作: JAVA_OPTS='-DgameLogicNamespace=foo.logic.mock' lein ring server-headless 8080 (ns foo.routes (:require [compojure.core :refer :all] [liberator.core :a

我在Clojure做一个项目。我已经定义了一系列路由,它们返回由其他名称空间中的逻辑计算的JSON数据。我希望能够以编程方式更改实现逻辑的名称空间,以便执行以下操作:

JAVA_OPTS='-DgameLogicNamespace=foo.logic.mock' lein ring server-headless 8080
(ns foo.routes
  (:require [compojure.core :refer :all]
            [liberator.core :as lib :refer [defresource request-method-in]]
            [liberator.representation :refer [ring-response]]))

(require
 (vec
  (cons (symbol (System/getProperty "gameLogicNamespace" "foo.logic.real"))
        '[:as logic])))
我现在是这样做的:

JAVA_OPTS='-DgameLogicNamespace=foo.logic.mock' lein ring server-headless 8080
(ns foo.routes
  (:require [compojure.core :refer :all]
            [liberator.core :as lib :refer [defresource request-method-in]]
            [liberator.representation :refer [ring-response]]))

(require
 (vec
  (cons (symbol (System/getProperty "gameLogicNamespace" "foo.logic.real"))
        '[:as logic])))
这很管用,但感觉有点笨重。有没有一种惯用的方法来实现我想要的

我的主要动机之一实际上是使用模拟数据进行单元测试路由,因此,如果有一个很好的解决方案只在测试中提供模拟逻辑(而不是作为JVM系统属性),那么建议是受欢迎的

我的主要动机之一实际上是使用模拟数据进行单元测试路由,因此,如果有一个很好的解决方案只在测试中提供模拟逻辑(而不是作为JVM系统属性),那么建议是受欢迎的

如果您还没有,请查看一些很好的实用程序来生成模拟请求,以测试您的环处理程序

如果您感兴趣的是提供在单元测试期间提供应用程序逻辑的功能的模拟版本,请考虑使用;它几乎是为这个目的定制的

(ns my-app.handlers-test
  (:require [clojure.test]
            [my-app.handlers :as h]
            [my-app.logic :as l]
            [ring.mock.request :as r]))

(deftest test-simple-handler
  (with-redefs [l/my-complicated-logic #(update-in % [:a] inc)]
    (is (= {:a 2}
           (h/my-handler (r/request :post "/foo" {:a 1}))))))