黄瓜浮点数

时间:2014-04-14 09:40:08

标签: int cucumber double

当我生成一个黄瓜短语时

然后我应该得到结果180.123

IntelliJ中生成的代码至少是

@Then("^I should have result (\\d+).(\\d+)$")
public void I_should_have_result_(int arg1, int arg2) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

有没有办法获得一个双倍而不必加入两个整数?

2 个答案:

答案 0 :(得分:1)

如果您将Gherkin步骤修改为

Then I should have result "180.23"

其实施将是

@Then("^I should have result \"([^\"]*)\"$")
public void I_should_have_result(String arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

很容易转换为浮点数。

答案 1 :(得分:0)

这是一个古老的问题,但是无论如何我都会在没有运气的情况下到处搜寻,所以我还是会回答。

您可以使用(\\d+\\.\\d+)来指定要加倍的广告:

在Java中:

@When("^I get a number (\\d+\\.\\d+)$")
public void iGetANumber(double my_number) {
    // Test code here
}

在Objective-C中(黄瓜):

When(@"^I get a number (\\d+\\.\\d+)$", ^(NSArray<NSString*>* args, NSDictionary* userInfo) {
    double my_number = [args[0] doubleValue];

    // Test code here
}

我相信您也可以使用(\\d+\\.\\d+)说明符,但将变量声明为float而不是double。至少当我尝试过时,它就可以在Java中工作。