无法从留言簿应用程序中的哈希图中获取密钥
Can't get keys from a hashmap in guestbook app
我正在玩 luminus 留言簿应用程序。
我在 g.test.db.core.clj
中添加了一些日志语句来查看数据结构的键。请看下面的 "Expected Keys" 和 "Actual Keys"。
(deftest test-messages
(jdbc/with-db-transaction [t-conn db/conn]
(jdbc/db-set-rollback-only! t-conn)
(let [timestamp (java.util.Date.)]
(is (= 1 (db/save-message!
{:name "Bobby"
:message "Hello World!"
:timestamp timestamp}
{:connection t-conn})))
(let [actual (-> (db/get-messages {} {:connection t-conn}))
expected {:name "Bobby"
:message "Hello World!"
:timestamp timestamp}]
(log/info "Expected Keys")
(pprint (keys expected))
(log/info "Actual Keys")
(pprint (keys actual)) ;;<--this is the problem
(is (= actual
expected))))))
"Expected Keys" 打印正常,但我得到 "Actual Keys" 的运行时异常:
[2017-03-04 14:31:28,076]Expected Keys
(:name :message :timestamp)
[2017-03-04 14:31:28,089]Actual Keys
lein test :only guestbook.test.db.core/test-messages
ERROR in (test-messages) (:) Uncaught exception, not in assertion.
expected: nil actual: java.lang.ClassCastException: null at [empty
stack trace]
lein test guestbook.test.handler
但是,如果我这样做:
(pprint actual)
我得到了我想要的:
({:id 35,
:name "Bobby",
:message "Hello World!",
:timestamp #inst "2017-03-04T04:31:01.030000000-00:00"})
这是怎么回事?为什么我不能打印从数据库返回的数据结构中的键?
看起来 actual 是一个列表而不是地图。尝试 (-> actual first keys pprint)
我正在玩 luminus 留言簿应用程序。
我在 g.test.db.core.clj
中添加了一些日志语句来查看数据结构的键。请看下面的 "Expected Keys" 和 "Actual Keys"。
(deftest test-messages
(jdbc/with-db-transaction [t-conn db/conn]
(jdbc/db-set-rollback-only! t-conn)
(let [timestamp (java.util.Date.)]
(is (= 1 (db/save-message!
{:name "Bobby"
:message "Hello World!"
:timestamp timestamp}
{:connection t-conn})))
(let [actual (-> (db/get-messages {} {:connection t-conn}))
expected {:name "Bobby"
:message "Hello World!"
:timestamp timestamp}]
(log/info "Expected Keys")
(pprint (keys expected))
(log/info "Actual Keys")
(pprint (keys actual)) ;;<--this is the problem
(is (= actual
expected))))))
"Expected Keys" 打印正常,但我得到 "Actual Keys" 的运行时异常:
[2017-03-04 14:31:28,076]Expected Keys (:name :message :timestamp)
[2017-03-04 14:31:28,089]Actual Keys
lein test :only guestbook.test.db.core/test-messages
ERROR in (test-messages) (:) Uncaught exception, not in assertion. expected: nil actual: java.lang.ClassCastException: null at [empty stack trace]
lein test guestbook.test.handler
但是,如果我这样做:
(pprint actual)
我得到了我想要的:
({:id 35,
:name "Bobby",
:message "Hello World!",
:timestamp #inst "2017-03-04T04:31:01.030000000-00:00"})
这是怎么回事?为什么我不能打印从数据库返回的数据结构中的键?
看起来 actual 是一个列表而不是地图。尝试 (-> actual first keys pprint)