-
TBH, I struggled a bit with writing my first actual test (for the spoke-and-chain demo site). <?php
it('finds two articles with matching keyword', function () {
// Show and submit the form
$response = $this->get('/search')
->form('#search-form')
->fill('q', 'Pine Mountain')
->submit();
// Result: count items
$response
->querySelector('.article-card')
->expect()
->toHaveCount(2);
// Result: assert text
expect($response->querySelector('.article-card')->getText())
->each()
->toContain('Pine Mountain');
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
A few notes that may make this easier to read (or more confusing…)? it('finds two articles with matching keywords') // all this could go inside a closure too, I just like the brevity of this
->get('/search') // the original get
->fill('q', 'Pine Mountain') // if this is the only form on the page you can skip the explicit `->form()` call
->submit() // submit that form
->expect() // start an expectation on the response
->statusCode->toBe(200) // I added this to make sure the form submission was successful, it could also be an `->assertOk()` before the expectation begins
->querySelector('.article-card')->toHaveCount(2) // check the count
->querySelector('.article-card')->text->each->toBe('Pine Mountain'); // check the text The trick here is where you put the expect(person)
.firstName.toBe('Michael')
.lastName.toBe('Bluth') |
Beta Was this translation helpful? Give feedback.
A few notes that may make this easier to read (or more confusing…)?