Clojure测试无法正常运行?

时间:2015-10-05 03:32:24

标签: database unit-testing clojure

我有一个单元测试的应用程序。在这个测试中,我正在创建一个用户 帐户,然后测试它。然后我修改它并测试修改后的 版。问题是,它似乎在做一些不按顺序的事情。上午 我错了?无论哪种方式,我该如何解决?

在我看来,当我运行测试时,然后在测试后修改数据库

在这个版本中,它可以工作 - 没有错误:

(ns myapp.db.account-test
  (:use expectations)
  (:require
    [myapp.db         :as DB]
    [myapp.db.account :as Account]
    ))

(defn- $reset-database
  "Reset the database to a pristine state."
  []
  (DB/create-db))

($reset-database)
(let [acct-1 {:email "one@email.com" :username "user-one" :password "goodpwd1"}
      acct-2 {:email "two@email.com" :username "user-two" :password "goodpwd2"}]
  (Account/create acct-1)
  (let [db-acct (Account/seek {:email (:email acct-1)})]
    (expect (:email    acct-1) (:email    db-acct))
    (expect (:username acct-1) (:username db-acct))

    (expect true  (Account/authenticate {:email (:email acct-1) :password "goodpwd1"}))
    (expect false (Account/authenticate {:email (:email acct-1) :password "badpwd"}))

  ;; Now try to change some attributes of the account
  #_(Account/modify acct-1 acct-2)
)

...然后

myapp 10:28 PM ~/Projects/myapp/myapp.net/code/myapp $ lein test myapp.db.account-testPicked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Rewriting src/cljx to target/generated/clj (clj) with features #{clj} and 0 transformations.
Rewriting src/cljx to target/generated/cljs (cljs) with features #{cljs} and 1 transformations.
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar

lein test myapp.db.account-test

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.

Ran 4 tests containing 4 assertions in 171 msecs
0 failures, 0 errors.

但如果我取消注释最后一行,那么'先前'测试就会失败。

  ...
  ;; Now try to change some attributes of the account
  (Account/modify acct-1 acct-2)
)

然后......

myapp 10:40 PM ~/Projects/myapp/myapp.net/code/myapp $ lein test myapp.db.account-testPicked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Rewriting src/cljx to target/generated/clj (clj) with features #{clj} and 0 transformations.
Rewriting src/cljx to target/generated/cljs (cljs) with features #{cljs} and 1 transformations.
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar

lein test myapp.db.account-test

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.

failure in (account_test.clj:21) : myapp.db.account-test
(expect
 true
 (Account/authenticate {:email (:email acct-1), :password "goodpwd1"}))

  act-msg: exception in actual: (Account/authenticate {:email (:email acct-1), :password "goodpwd1"})
    threw: class java.lang.NullPointerException -
           com.lambdaworks.crypto.SCryptUtil$check (SCryptUtil.java:74)
           on (core.clj:41)
           on (account.clj:109)
           on (account_test.clj:21)

failure in (account_test.clj:22) : myapp.db.account-test
(expect
 false
 (Account/authenticate {:email (:email acct-1), :password "badpwd"}))

  act-msg: exception in actual: (Account/authenticate {:email (:email acct-1), :password "badpwd"})
    threw: class java.lang.NullPointerException -
           com.lambdaworks.crypto.SCryptUtil$check (SCryptUtil.java:74)
           on (core.clj:41)
           on (account.clj:109)
           on (account_test.clj:22)

Ran 4 tests containing 4 assertions in 234 msecs
0 failures, 2 errors.

如果重要,myapp.db.account的相关部分是:

(defn seek
  "Get an account by email address"
  [{:keys [email]}]
  (row-to-hash (get-account-by-email {:email email})))

(defn modify
  "Modify an account."
  [{current-email :email}
   {new-email     :email new-username :username new-password :password}]

  (jdbc/with-db-transaction [connection db-env/spec]
    (let [current (seek {:email current-email})]

      (update-email-by-email! {:current_email current-email :new_email new-email}))))

(defn authenticate
  "Try to authenticate given an identifier and a password.
  Note that this returns a false case in the case of actual falsity, but nil
  in the case of a poorly formed query (such as missing credentials)"
  [{:keys [id email username password]}]
  (and
    password
    (scrypt/verify
      ;; compare this password attempt
      password
      ;; with this hash from the db (when possible)
      (:passhash
        (first
          (cond
            ;; Branch on which field is present.
            id       (get-authorization-fields-by-id       {:id id})
            email    (get-authorization-fields-by-email    {:email email})
            username (get-authorization-fields-by-username {:username username})
            ;; Default to false
            :else nil))))))

1 个答案:

答案 0 :(得分:1)

你不应该运行lein test,你应该使用lein expectations(这需要你的project.clj中的一个插件):

:plugins [[lein-expectations "0.0.7"]]
在jvm shutdown时调用

expectations。我的猜测是lein test正在编译你的文件,它会评估你的数据库修改,并在此之后运行期望值。没关系,直到你介绍了你想要在期望之后运行的变化。

要解决此问题,请在您的期望中包含您要运行的代码。以下是使用上述代码的示例。它使用' more->来自expect库的宏。

(let [acct-1 {:email "one@email.com" :username "user-one" :password "goodpwd1"}
      acct-2 {:email "two@email.com" :username "user-two" :password "goodpwd2"}]

  (expect 
    (more->
      true (do ($reset-database)
               (Account/create acct-1)
               (Account/authenticate {:email (:email acct-1) :password "goodpwd1"}))
      false (do ($reset-database)
                (Account/create acct-1)
                (Account/authenticate {:email (:email acct-1) :password "badpwd"})))))