2012-08-15 10 views
13

Beklenen bir özel durum getiren bir işlevi nasıl test edebilirim? İşte istisna atar fonksiyonudur:Beklenen bir özel durumla başa çıkmak için nasıl bir sınama yazabilirim?

(defn seq-of-maps? 
    "Tests for a sequence of maps, and throws a custom exception if not." 
    [s-o-m] 
    (if-not (seq? s-o-m) 
    (throw (IllegalArgumentException. "s-o-m is not a sequence")) 
    (if-not (map? (first s-o-m)) 
     (throw (IllegalArgumentException. "s-o-m is not a sequence of maps.")) 
     true))) 

ben bir istisna atılır ve yakalanmış ve daha sonra karşılaştırılır edildiği aşağıdaki gibi bir testi tasarım istiyorum. Açıkçası

Testing util.test.core 

FAIL in (test-seq-of-maps) (core.clj:27) 
expected: (not (= excp1 "s-o-m is not a sequence")) 
    actual: (not (not true)) 

Ran 2 tests containing 6 assertions. 
1 failures, 0 errors.  

Testleri yazma hakkında bir şey eksik:

(deftest test-seq-of-maps 
    (let [map1 {:key1 "val1"} 
     empv [] 
     s-o-m (list {:key1 "val1"}{:key2 "val2"}) 
     excp1 (try 
       (seq-of-maps? map1) 
       (catch Exception e (.getMessage e)))] 
    (is (seq-of-maps? s-o-m)) 
    (is (not (= excp1 "s-o-m is not a sequence"))))) 

Bu hataları alıyorum: Aşağıdaki çalışmaz. Bunu anlamakta zorlanıyorum. Projem lein le ile kuruldu ve testleri lein testi ile yapıyorum.

Teşekkürler.

+0

olası bir kopyası [Bir birim sınamasında nasıl bir hata bekleyebilirim?] (Http://stackoverflow.com/questions/7852092/how-do-i-expect-failure-in-a-unit-test) –

cevap

15

Testinizdeki son iddia yanlış; map1 haritaların bir sırası olmadığı için (is (= excp1 "s-o-m is not a sequence")) olmalıdır. Bunun dışında, atılan özel durumları denetlemek için (is (thrown? ..)) veya (is (thrown-with-msg? ...)) kullanmak muhtemelen daha nettir.

+0

Kullanmak için +1 (atıldı mı? ...) – Alex

İlgili konular