【问题标题】:How do I get emacs cider (clojure mode) to use my test environment variables when running tests?如何让 emacs cider(clojure 模式)在运行测试时使用我的测试环境变量?
【发布时间】:2023-03-25 02:26:01
【问题描述】:

我已经为开发和测试环境设置了单独的database-url,当在 REPL 和命令行上的lein test 运行我的 web 应用程序时,这很有效。这是我的profiles.clj

{:profiles/dev  {:env {:database-url "wiki"}}
 :profiles/test {:env {:database-url "wiki-test"}}}

以及正确的数据库实例被击中的证据(我正在使用 CouchDB):

;; Running the site from the REPL:
[info] [<0.12149.0>] 127.0.0.1 - - GET /wiki/home-page 200
[info] [<0.10353.0>] 127.0.0.1 - - GET /wiki/about 200

;; Running lein test:
[info] [<0.12026.0>] 127.0.0.1 - - GET /wiki-test/welcome 404
[error] [<0.12933.0>] Could not open file /usr/local/var/lib/couchdb/wiki-test.c

但是,当我在 Emacs 中通过 Cider 运行测试时,它使用了开发环境,因此使用了错误的数据库实例。

我该如何解决这个问题?

【问题讨论】:

    标签: emacs clojure leiningen cider


    【解决方案1】:

    我建议你尝试使用with-redefs

    类似这样的:

    (with-redefs [db (get-my-test-db)]
      (run-my-tests)
    

    db 是您在测试中绑定数据库句柄的符号。

    这篇文章应该会有所帮助: Isolating External Dependencies in Clojure

    【讨论】:

    • 谢谢,我想我可以看到它是如何工作的,但它不会使用测试环境对吗?也许另一种方法是让 Cider 默认使用测试环境?
    • 这行得通。我将函数调用 with-redefs 放入一个夹具中,以便在每次测试时调用它。
    猜你喜欢
    • 2018-03-05
    • 2014-10-06
    • 1970-01-01
    • 2021-06-12
    • 2022-11-20
    • 1970-01-01
    • 2023-04-05
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多