Skip to content

Commit 441b43f

Browse files
authored
system-specs: Update "Gotchas" section about asserting database state (#50)
1 parent aa89202 commit 441b43f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/03-code-internals/20-system-specs.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,42 @@ This works fine in our Ember SPA for the initial navigation and page load. Howev
175175
page.find(".some-link").click
176176
```
177177

178+
### Avoiding direct assertions against database records
179+
180+
Asserting directly against database records within system specs may lead to inconsistent test results if you don't appropriately wait for the backend updates to synchronize with the frontend.
181+
182+
For example, directly checking database states like this in system tests can be problematic:
183+
184+
```ruby
185+
expect(Topic.count).to eq(2)
186+
```
187+
188+
This assertion assumes that the backend update has already occurred, ignoring any asynchronous behavior.
189+
190+
To avoid such issues:
191+
192+
1. **Observe Changes in the UI**
193+
System specs should focus on what the user can observe via the user interface. Use Capybara's built-in matchers to wait for DOM updates. For example:
194+
195+
```ruby
196+
expect(page).to have_selector(".success-message")
197+
```
198+
199+
or
200+
201+
```ruby
202+
expect(topic_page).to have_topic(topic)
203+
```
204+
205+
2. **Wait for Backend Synchronization**
206+
If necessary, use a helper like `try_until_success` to check database state explicitly but sparingly:
207+
208+
```ruby
209+
try_until_success do
210+
expect(Topic.count).to eq(1)
211+
end
212+
```
213+
178214
### Page Objects
179215

180216
To make querying and inspecting parts of the page easier and reusable in between system specs, we are using the concept of Page Objects. A basic Page Object looks like this:

0 commit comments

Comments
 (0)