为了满足并发要求,我想知道如何在 Godog
中的多个步骤之间传递参数或状态。
func FeatureContext(s *godog.Suite) {
// This step is called in background
s.Step(`^I work with "([^"]*)" entities`, iWorkWithEntities)
// This step should know about the type of entity
s.Step(`^I run the "([^"]*)" mutation with the arguments:$`, iRunTheMutationWithTheArguments)
我想到的唯一想法是内联被调用的函数:
state := make(map[string]string, 0)
s.Step(`^I work with "([^"]*)" entities`, func(entityName string) error {
return iWorkWithEntities(entityName, state)
})
s.Step(`^I run the "([^"]*)" mutation with the arguments:$`, func(mutationName string, args *messages.PickleStepArgument_PickleTable) error {
return iRunTheMutationWithTheArguments(mutationName, args, state)
})
但这感觉有点像解决方法。 Godog
库本身有什么功能可以传递这些信息吗?
最佳答案
我在步骤中使用方法而不是函数找到了好运。然后,将状态放入结构中。
func FeatureContext(s *godog.Suite) {
t := NewTestRunner()
s.Step(`^I work with "([^"]*)" entities`, t.iWorkWithEntities)
}
type TestRunner struct {
State map[string]interface{}
}
func (t *TestRunner) iWorkWithEntities(s string) error {
t.State["entities"] = s
...
}
关于Godog 在步骤之间传递参数/状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948850/