【发布时间】:2018-03-04 01:22:03
【问题描述】:
尝试使用 PHP 和 Behat 创建功能测试,我想要一个随机字符串作为电子邮件,但我希望字符串在每次新测试运行时都是随机的,但我想将创建的相同字符串传递给不同的测试作为参数。
因此,如果我生成一个 10 位字符串,我想生成该字符串,然后将其作为相同的 10 位序列通过其他测试
我是 PHP 新手,所以我不太确定如何设置它,但这是我目前在 Behat 上下文文件中所拥有的内容
class FeatureContext implements Context {
private $MailPage;
private $registrationpage;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct(MailPage $mailPage, RegistrationPage $registrationpage)
{
// Page obects injected directly in constructor with type hints.
$this->MailPage = $MailPage;
$this->registrationpage = $registrationpage;
}
/**
* @BeforeSuite
*/
public static function generateRandomString() {
// Generate random string for Email implementation.
$randomString = bin2hex(openssl_random_pseudo_bytes(10));
return $randomString;
}
/**
* @Given /^I register a temporary email address $/
*/
public function iRegisterATemporaryEmailAddress()
{
$this->MailPage->grabNewEmail(self::generateRandomEmail());
}
/**
* @Given /^I register a Developer account$/
*/
public function iRegisterADeveloperAccount()
{
$this->registrationpage->fillInFormDetails(self::generateRandomEmail());
}
我遇到的问题是,在参数不变的情况下,每次调用它都会生成一个不同的字符串,但我只希望它为整个套件生成一次。有什么想法吗?
【问题讨论】:
-
不完全确定我是否理解大局,但您可以在构造函数中分配 $this->randomString,然后简单地引用 randomString 属性而不是 generateRandomString 方法
标签: php string random parameters behat