我有一种情况,如下所示:

scenario("Websocket client sends data to websocket server on SAP when is UP") {
  Given("Websocket server on SAP is ACTIVE")
  And("it supports websocket channel")
  When("Websocket client get started")
  Then("print message `Connection has been successfully established`")

  Given("Websocket server on SAP is ACTIVE")
  And("it does not support websocket channel")
  When("Websocket client get started")
  Then("throws RunException")
  succeed

}


如您所见,Given重复两次。问题是,如果我只留下第二个Given,是否也正确?

scenario("Websocket client sends data to websocket server on SAP when is UP") {
  Given("Websocket server on SAP is ACTIVE")
  And("it supports websocket channel")
  When("Websocket client get started")
  Then("print message `Connection has been successfully established`")

  And("it does not support websocket channel")
  When("Websocket client get started")
  Then("throws RunException")
  succeed

}

最佳答案

通常,让场景依赖其他场景来设置其上下文通常是一个坏主意。这就是我们称为“ GivenScenario”的模式。这使行为很难看清(您现在必须阅读第一个方案的全部内容才能理解第二个方案的上下文),并且如果第一个方案失败,则第二个方案甚至都不会运行。

不知道第二种情况是否依赖第一种情况的人也可能会更改第一种情况或在它们之间添加一个。

如果您有某种“永远在线”的行为,则大多数BDD工具都会将此称为“背景”。 ScalaTest中的等效项看起来像“ BeforeAndAfter”特征(我对ScalaTest并不熟悉,所以如果我错了,有人可以纠正我!)。

因此,您可以将行为移至“之前”,而不是在方案中“始终打开”活动的Websocket。

当然,ScalaTest中的“背景”在您运行时不会打印。没声音但是您仍然可以通过调用它来解决此问题,并将英语移动到任一标题(请注意添加了“ active”):

class ExampleSpec extends FeatureSpect with BeforeAndAfter {

  before {
    server = startServer()
  }

  scenario("Websocket client sends data to active websocket server on SAP when is UP") {
    Given("it supports websocket channel")
    // etc...
  }
}


或第一个给定的:

class ExampleSpec extends FeatureSpect with BeforeAndAfter {

  before {
    server = startServer()
  }

  scenario("Websocket client sends data to websocket server on SAP when is UP") {

    Given("an active server with a client that supports websocket channel")
    // etc...
  }
}


(再次,对ScalaTest并不熟悉,自从我编写Scala以来,这一直都是永久的,所以请更正所有语法错误;此答案更着重于原理。The documentation I found on this将所有步骤都显示为小写。)

我倾向于使用GivenScenarios的唯一时间是当我懒惰(或务实)并且存在(通常是人为的)交互作用时,将导致对成功进行一次或多次尝试:

Given Florence Forgetful is at the login page
When she puts in the wrong username
Then she should be told there was an error
When she puts in the wrong password
Then she should be told there was still an error
When she puts in the right username and right password
Then she should be taken to her home page.


但是,如果这样的可读性不佳,或者在第一步中的交互第一次变得不那么琐碎(例如,您还必须填写验证码),或者第一次出现错误时,这些步骤之一,我将把它们重构为单独的场景。

如果打开和关闭客户端中的支持是人类可以执行的操作,那么您可以遵循相同的模式,但是连续需要两个“ whens”是一个好兆头,表明现在不止一个功能在这里说明:

scenario("Websocket client sends data to websocket server on SAP when is UP") {
  Given("Websocket server on SAP is ACTIVE")
  And("websocket support is turned off")
  When("Websocket client get started")
  Then("throws RunException")
  When("websocket support is turned on")
  And("Websocket client get started") // <-- This is a second "When" here
  Then("print message `Connection has been successfully established`")
  succeed
}


因此,可能不是正确的方法。如有疑问,请完全避免使用GivenScenario模式。

关于scala - 在一种情况下给出多个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55787072/

10-11 08:17