When I set out to write expectations I wanted to create a simple unit testing framework. I'm happy with what expectations provides for unit testing; however, I also need to write the occasional test that changes values or causes a side effect. There's no way I could go back to clojure.test after enjoying better failure messages, trimmed stack traces, automatic testing running, etc. Thus, expectations.scenarios was born.
Using expectations.scenarios should be fairly natural if you already use expectations. The following example is a simple scenario (which could be a unit test, but we'll start here for simplicity).
(ns example.scenarios (:use expectations.scenarios)) (scenario (expect nil? nil))A quick trip to the command line shows us that everything is working as expected.
Ran 1 tests containing 1 assertions in 4 msecs 0 failures, 0 errors.As I said above, you could write this test as a unit test. However, expectations.scenarios was created for the cases in which you want to verify a value, make a change, and verify a value again. The following example shows multiple expectations verifying changing values in the same scenario.
(scenario (let [a (atom 0)] (swap! a inc) (expect 1 @a) (swap! a inc) (expect 2 @a)))In expectations (unit tests) you can only have one expect (or given) so failures are captured, but they do not stop execution. However, due to the procedural nature of scenarios, the first failing expect stops execution.
(scenario (let [a (atom 0)] (swap! a inc) (expect 2 @a) (println "you'll never see this"))) failure in (scenarios.clj:4) : example.scenarios (expect 2 (clojure.core/deref a)) expected: 2 was: 1 on (scenarios.clj:7) Ran 1 tests containing 1 assertions in 81 msecs 1 failures, 0 errors.expectations.scenarios also allows you to easily verify calls to any function. I generally use interaction expects when I need to verify some type of side effect (e.g. logging or message publishing).
(scenario (println "1") (expect (interaction (println "1"))))It's important to note the ordering of this scenario. You don't 'setup' an expectation and then call the function. Exactly the opposite is true - you call the function the same way you would in production, then you expect the interaction to have occurred. You may find this jarring if you're used to setting up your mocks ahead of time; However, I think this syntax is the least intrusive - and I think you'll prefer it in the long term.
The above example calls println directly, but your tests are much more likely to look something like this.
(defn foo [x] (println x)) (scenario (foo "1") (expect (interaction (println "1"))))Similar to all other mocking frameworks (that I know of) the expect is using an implicit "once" argument. You can also specify :twice and :never if you find yourself needing those interaction tests.
(defn foo [x] (println x)) (scenario (foo "1") (foo "1") (expect (interaction (println "1")) :twice) (expect (interaction (identity 1)) :never))On occasion you may find yourself interested in verifying 2 out of 3 arguments - expectations.scenarios provides the 'anything' var that can be used for arguments you don't care about.
(defn foo [x y z] (println x y z)) (scenario (foo "1" 2 :a) (expect (interaction (println "1" anything :a))))That's about all there is to expectations.scenarios, hopefully it fills the gap for tests you want to write that simply can't be done as unit tests.