Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stub langohr RabbitMQ interactions in clojure?

I'm trying to stub the RabbitMQ interactions, as those aren't really the main purpose of the application I'm writing.

So, I've tried rebinding the langohr functions in my tests like so:

(defn stub [ch]
  (langohr.basic/ack ch 1))

(deftest test-stub
  (with-redefs [langohr.basic/ack (fn [a1 a2] true)]
    (is (= true (stub "dummy")))))

When I run the test with lein test, I get a

java.lang.ClassCastException:
redwood.env_test$fn__2210$fn__2211 cannot be cast to clojure.lang.IFn$OLO

I've been trying several other ways including different test frameworks to redefine or rebind the langohr lib functions with no progress.

I've tested other scenarios and I've successfully stubbed cheshire (json parsing clojure lib) functions with the above code structure. I humbly request assistance in understanding why my langohr stubs aren't working and for tips on how I can do this in an elegant manner.

like image 406
Phong Avatar asked Jan 22 '13 15:01

Phong


1 Answers

The ClassCastException occurs because langohr.basic/ack is a function that takes a primitive argument - specifically, it is of type clojure.lang.IFn$OLO, where the OLO stands for "object, long, object".

You have to redef it be of the same type. Try this:

(with-redefs [langohr.basic/ack (fn [a1 ^long a2] true)] ...)
like image 81
Chris Perkins Avatar answered Sep 29 '22 04:09

Chris Perkins