2012-07-06 66 views
1

如何測試具有多輸入/預期輸出的功能?EasyB測試多輸入/輸出值

這是一個非常簡單的例子:

scenario "Can add two numbers", { 
    given "Two numbers", { 
     num1 = 2 
     num2 = 3 
    } 

    when "I trigger add.", { 
     result = add(num1,num2) 
    } 

    then "The result should be correct.", { 
     result.shouldBe 5 
    } 
} 

我想與多個值來測試這一點,說add(4,8).shouldBe 12, ....

請告訴我要做到這一點的最佳做法?在其他BDD框架中,我已經看到類似於表的結構來實現此功能,但在EasyB中找不到類似結構。我應該創建多個場景來覆蓋這個場景(將場景名稱附加(1),(2)),還是應該將輸入和預期輸出放入數組中,並檢查它是否相等? 如果我使用後一種方法,我該如何獲得意義上的失敗?

回答

1

使用where /例子子句 http://code.google.com/p/easyb/wiki/ChangesInEasyb098

package org.easyb.where 

/* 
Example tests a map at the story level 
*/ 

numberArray = [12, 8, 20, 199] 

where "we are using sample data at a global level", [number:numberArray] 

before "Before we start running the examples", { 
    given "an initial value for counters", { 
    println "initial" 
    whenCount = 0 
    thenCount = 0 
    numberTotal = 0 
    } 
} 

scenario "Number is #number and multiplier is #multiplier and total is #{number * multiplier}", { 
    when "we multiply #number by #multiplier", { 
    whenCount ++ 
    num = number * multiplier 
    } 
    then "our calculation (#num) should equal #{number * multiplier}", { 
    num.shouldBeGreaterThan 0 
    numberTotal += num 
    thenCount ++ 
    } 
    where "Multipliers should be", { 
    multiplier = [1,2,3] 
    } 
} 


after "should be true after running example data", { 
    then "we should have set totals", { 
    whenCount.shouldBe 12 
    thenCount.shouldBe 12 
    num = 0 
    numberArray.each { n -> 
     num = num + (n + (2*n) + (3*n)) 
    } 

    num.shouldBe numberTotal 
    } 
} 
+0

感謝FO建議發行票據。它在手冊中缺失。但我認爲編碼和閱讀有點困難,然後「桌面驅動」(如黃瓜)。 – matcauthon 2012-07-11 10:43:20

+0

是的,我認爲這是以這個例子的方式壓縮故事和步驟的代價。它比黃瓜更像黃瓜/ rspec組合。 – KarlM 2012-07-12 01:56:21