How should I deal with a \n printed by `with-out-str` at the end of the output in a Clojure test?

Viewed 83

I'm writing a test to check if the output printed to the console matches the JSON string I expect.

(deftest create-account-test
  (testing "Create account"
    (is (= (str (cheshire/generate-string {:account {:active-card true :available-limit 100} :violations []}))
           (with-out-str
             (io/-main "test/sample-input.txt"))))))

The test breaks due to a mismatch: the \n that comes at the end of the actual output:

actual: "{\"account\":{\"active-card\":true,\"available-limit\":100},\"violations\":[]}\n"          
diff: - "{\"account\":{\"active-card\":true,\"available-limit\":100},\"violations\":[]}"          
      + "{\"account\":{\"active-card\":true,\"available-limit\":100},\"violations\":[]}\n"

How should I deal with this newline at the end?

I considered concatenating a \newline with the expected string, but I guess there's a better solution.

1 Answers

You can do like this

(deftest create-account-test
  (testing "Create account"
    (is (= {:account {:active-card true :available-limit 100} :violations []}
           (-> (with-out-str (io/-main "test/sample-input.txt"))
               json/read-string))))))

but if you have a good reason to compare strings, simply add a \n into it.

(deftest create-account-test
  (testing "Create account"
    (is (= (str (cheshire/generate-string {:account {:active-card true :available-limit 100} :violations []}) "\n")
           (with-out-str
             (io/-main "test/sample-input.txt"))))))
Related