現在我正在重寫一些單元測驗來使用 Pest,并注意到每個測驗都會創建一個新用戶。測驗需要創建用戶回傳的 id。我想知道是否可以把它放在 Pest 提供的 beforeEach 函式中,這樣我就可以在我的測驗中訪問這個用戶 ID。
我想在我的測驗中訪問 $user,這可能嗎?如果是這樣,怎么做?我注意到在帶有 Jest(類似)的 Javascript 中,您可以在 BeforeEach 之前初始化變數,但這似乎在 php 中不起作用。
幫助將不勝感激!
beforeEach(function () {
$user = User::factory()->create();
});
test('Test that it shows finished tasks of company', function () {
// Do something with the user variable
//assertion
});
uj5u.com熱心網友回復:
來自https://pestphp.com/docs/setup-and-teardown#beforeeach的檔案
像往常一樣,函式中的
$this變數beforeEach系結到當前的測驗用例物件。因此,您可以與它和測驗函式共享資料。
beforeEach(function () {
$this->user = User::factory()->create();
});
test('Test that it shows finished tasks of company', function () {
// You should have access to the user variable as a property:
dump($this->user);
// assertion
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/525592.html
