2013-06-12 9 views
6

kullanarak atanan bir işlevi alay etmek için midje'yi kullanamıyorum: Durum şu: Ben B fonksiyonunu çağıran birim sınama işlevi A çalışıyorum. B fonksiyonu, bir sapan try + bloğunda ve belirli koşullar altında çağrılır Bir sapan atışı + kullanarak atabilir. Midje testinde B fonksiyonu ile uğraşmak istiyorum, böylece try + bloğundaki yakalamanın aslında yakalayacağı bir şey döndürüyor. Ama atmak için doğru şeyi yaratamam gibi görünmüyor. İşte kod ve testin esas kısaltılmış taslak verilmiştir:Neden slingshot'ın atışı +

(defn function-A 
    [param] 
    (try+ 
    (function-B param) 
    (catch [:type :user-not-found] 
     (do-something)))) 

(defn function-B 
    [param] 
    (throw+ [:type :user-not-found])) 

(fact "do-something is called" 
    (function-A "param") => (whatever is the result of calling do-something) 
    (provided 
    (function-B "param") =throws=> (clojure.lang.ExceptionInfo. "throw+: {:type :user-not-found}" 
                   {:object {:type :user-not-found}, :environment {}} 
                   nil))) 

Ben atma olduğumu ExceptionInfo roughtly doğru şey gibi görünüyor. Uygulamam çok sayıda prn ifadesiyle çalışırken bunu görebiliyorum. Bununla birlikte, ne çalışırsam çalışırım, testin işe yaramam.

Ayrıca, sorunu anlayabildiğimi görmek için bir kodda aşağıdaki kodun birazını da denedim. Bununla birlikte, her iki kod parçası da aynı İstisnaları içermekle birlikte, sadece bir tanesi (saf sapan olanı) yakalayıp "yakala" yazmayı başarır. Birinin neden çalıştığını ve diğerinin neden olmadığını anlayabilseydim, problemi birim testi ile çözebileceğimi düşünüyorum.

(try+ 
    (try 
    (throw+ {:type :user-not-found}) 
    (catch Exception e 
     (prn "Caught: " e) 
     (prn "Class: " (.getClass e)) 
     (prn "Message: " (.getMessage e)) 
     (prn "Cause: " (.getCause e)) 
     (prn "Data: " (.getData e)) 
     (throw e))) 
    (catch [:type :user-not-found] p 
    (prn "caught it"))) 

(try+ 
    (try 
    (throw (clojure.lang.ExceptionInfo. "throw+: {:type :user-not-found}" 
             {:object {:type :user-not-found}, :environment {}} 
             nil)) 
    (catch Exception e 
     (prn "Caught: " e) 
     (prn "Class: " (.getClass e)) 
     (prn "Message: " (.getMessage e)) 
     (prn "Cause: " (.getCause e)) 
     (prn "Data: " (.getData e)) 
     (throw e))) 
    (catch [:type :user-not-found] p 
    (prn "caught it"))) 

cevap

2

(here, here ve here bakınız) bir throwable oluşturulan nasıl sapan kodunu takiben, sadece throw ing çalışacak bir Atılabil üretmek için aşağıdaki (biraz yapmacık) yolunu buldu.

(s/get-throwable (s/make-context {:type :user-not-found} "throw+: {:type :user-not-found}" (s/stack-trace) {})) 

Örneğinizden beklediğiniz sonucu ortaya koyar.

(try+ 
    (try 
    (throw (s/get-throwable (s/make-context {:type :user-not-found} "throw+: {:type :user-not-found}" (s/stack-trace) {}))) 
    (catch Exception e 
     (prn "Caught: " e) 
     (prn "Class: " (.getClass e)) 
     (prn "Message: " (.getMessage e)) 
     (prn "Cause: " (.getCause e)) 
     (prn "Data: " (.getData e)) 
     (throw e))) 
    (catch [:type :user-not-found] p 
    (prn "caught it"))) 

Yardım edin. Aşağıdaki çözümü konusunda gerçekten geç bir yanıt ama ne var

+1

Bu harika. Kesinlikle işe yarıyor. Ünite test kodumda, sadece = throws => (slingshot-exception {: type: user-not-found} 'demek için bir fonksiyon yarattım. Fonksiyon: = (sapan-istisna [istisna-harita] ] (slingshot.support/get-throwable (slingshot.support/make-context istisna-haritası (str "throw +:" harita) (slingshot.support/stack-trace) {}))). Sorunumu çözdüğünüz için çok teşekkürler. –

3

:

(defn ex+ [cause] 
    (try 
    (throw+ cause) 
    (catch Throwable ex 
     ex))) 

Kullanım örneği:

(broken-fn) =throws=> (ex+ {:type :user-not-found}) 

yararı Sapan iç uygulaması güvenmeyin olmasıdır.

+1

Bu kabul edilen cevap olmalıdır. Basit ve sağlamdır, çünkü ana sapan API'ye dayanır. – neverfox

İlgili konular