2017-05-08 103 views
0

我想創建一個簡單的計算器應用程序使用黃瓜/小黃瓜和斯卡拉信息是在數據表中給出但不知道如何去做這件事?我很新的BDD測試,並想知道其他程序員如何克服這一點我已經創建了一個功能文件與其他操作,如加法,減法,乘法和除法,其中的值是在'什麼時候提供,然後'語句工作正常,但不知道如何使用Scala處理Cucumber內的數據表。與黃瓜和斯卡拉Datatable測試

任何幫助,將不勝感激

特性文件:

Scenario Outline: Addition 
Given my calculator is running 
When I add <inputOne> and <inputTwo> 
Then result should be equal to <output> 
Examples: 
    | inputOne | inputTwo | output | 
    | 20  | 30  | 50  | 
    | 2  | 5  | 7  | 
    | 0  | 40  | 40  | 

步驟定義文件:

class CalcSteps extends ScalaDsl with EN { 

var calc: MyCalc = _ 
var result: Int= _ 

Given("""^my calculator is running$""") {() => 
calc = new MyCalc 
} 

When("^I add \"(.*?)\" and \"(.*?)\":$") { (firstNum: Int, secondNum: Int, values: DataTable) => 
//not sure what to do here 
//result = calc.add(firstNum, secondNum) 
} 

Then("^result should be equal to \"(.*?)\"$") { (expectedResult: Int) => 
assert(result == expectedResult, "Incorrect result of calculator computation") 

}

MyCalc:

class MyCalc { 

def add(first:Int, second: Int): Int = { 
    first + second 
    } 
} 
+1

你應該使用'場景大綱',這樣每個添加都是測試。所以在你的情況下,你將有3個測試。 Datatable用於傳遞單個測試所需的詳細信息。我對Scala一無所知,但由於它基於Java,因此您使用的步驟定義不正確。在Java中,您必須使用DataTable將值作爲一種2 dim陣列來獲取。您甚至可以使用List或Map來代替。 – Grasshopper

+0

感謝關於情景大綱的提醒。我現在糾正了這個問題。你碰巧有一個在java中使用數據表的例子嗎?謝謝 – user610

+1

你不需要數據表代碼了...你的原代碼應該可以工作。從步驟定義中刪除「,values:DataTable」。 – Grasshopper

回答

0

感謝@Grasshopper我能夠通過簡單地改變特性文件來解決問題:從步驟定義導致現場:

Scenario Outline: Addition 
Given my calculator is running 
When I add <inputOne> and <inputTwo> 
Then result should be equal to <output> 
Examples: 
    | inputOne | inputTwo | output | 
    | 20  | 30  | 50  | 
    | 2  | 5  | 7  | 
    | 0  | 40  | 40  | 

,並去掉「數據表值」

When("^I add \"(.*?)\" and \"(.*?)\":$") { (firstNum: Int, secondNum: Int) => 

result = calc.add(firstNum, secondNum) 
}