我有一个带有单元测试的应用程序。在这个测试中,我创建了一个用户帐户,然后对它进行测试。然后我修改它并测试修改后的版本。问题是,它似乎是在做一些不正常的事情。我说错了吗?不管怎样,我该怎么解决呢?
在我看来,当我运行测试时,在测试之后修改数据库。
在这个版本中,它是有效的--没有错误:
(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))))))发布于 2015-10-05 04:15:58
您不应该运行lein test,您应该使用lein expectations (这需要project.clj中的插件):
:plugins [[lein-expectations "0.0.7"]]expectations在jvm关闭时被调用。我的猜测是,lein test正在编译您的文件,该文件评估您对db的修改,期望在此之后运行。在您介绍您想要按照您的期望运行的更改之前,这是很好的。
要解决这个问题,请包含您希望在预期范围内运行的代码。下面是一个使用上面代码的示例。它使用expect库中的“macro >宏”.
(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"})))))https://stackoverflow.com/questions/32940889
复制相似问题