【问题标题】:How to stop JBehave from trimming multiline parameters?如何阻止 JBehave 修剪多行参数?
【发布时间】:2017-11-13 17:13:26
【问题描述】:

我有一个步骤需要检查某些消息。我使用了多行参数:

Then I see welcome message: 'Welcome to our site! 
Some more text 
Even more text'

问题是消息应该在第一行和第二行的末尾有空格(IDK 为什么,但它应该)。 JBehave 会修剪每一行的空格。所以测试失败了。

如何设置不修整这个参数?

我试过写 \n 代替实际的换行符,但它没有被换行符取代

还尝试在参数的开头添加 {trim=false},但它会将其作为参数的一部分读取。

用引号做了一些实验(使用了“,',无),它也没有帮助......

【问题讨论】:

    标签: bdd jbehave


    【解决方案1】:

    我已经测试过了,似乎 JBehave 实际上在测试结束时截断了空格:

    When some step with multitline parameter:first line has 10 spaces at the end          
    Second line has 6 spaces at the end      
    Third line has no spaces at the end, but fourth line has 8 spaces
    
    Fifth line has 3 spaces, and Sixth line has only 7 spaces   
    

        @When("some step with multitline parameter:$lines")
        public void stepWithMultilneString(String s) {
    
            String lines [] = s.split("\\r?\\n");
    
            for(int i = 0; i < lines.length; i++) {
                System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
            }
        }
    
    Line 1 is: >>first line has 10 spaces at the end          <<
    Line 2 is: >>Second line has 6 spaces at the end      <<
    Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
    Line 4 is: >>        <<
    Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces<<
    When some step with multitline parameter:first line has 10 spaces at the end          
    Second line has 6 spaces at the end      
    Third line has no spaces at the end, but fourth line has 8 spaces
    
    Fifth line has 3 spaces, and Sixth line has only 7 spaces
    

    我建议绕过——在文本的开头和结尾使用分隔符! 的步骤:

    When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
    Second line has 6 spaces at the end      
    Third line has no spaces at the end, but fourth line has 8 spaces
    
    Fifth line has 3 spaces, and Sixth line has only 7 spaces   
    
    !
    

        @When("step with multitline parameter surrounded by !:$string")
        public void stepWithMultilneStringVersion2(String s) {
    
            if( s.startsWith("!")) {
                s = s.substring(1);
            }
            if( s.endsWith("!")) {
                s = s.substring(0, s.length()-1);
            }
    
            String lines [] = s.split("\\r?\\n");
    
            for(int i = 0; i < lines.length; i++) {
                System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
            }
        }
    
    Line 1 is: >>first line has 10 spaces at the end          <<
    Line 2 is: >>Second line has 6 spaces at the end      <<
    Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
    Line 4 is: >>        <<
    Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces   <<
    Line 6 is: >>       <<
    When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
    Second line has 6 spaces at the end      
    Third line has no spaces at the end, but fourth line has 8 spaces
    
    Fifth line has 3 spaces, and Sixth line has only 7 spaces   
    
    !
    

    【讨论】:

    • 谢谢,幸运的是最后一行在我的例子中没有空格。看起来线条被 IntelliJ 截断了。我在每行的末尾添加了保护符号 (some text ~\n) '~' 然后在步骤正文中替换为任何内容。
    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2017-07-12
    • 2014-08-03
    相关资源
    最近更新 更多