You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/03-code-internals/20-system-specs.md
+36Lines changed: 36 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -175,6 +175,42 @@ This works fine in our Ember SPA for the initial navigation and page load. Howev
175
175
page.find(".some-link").click
176
176
```
177
177
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
+
178
214
### Page Objects
179
215
180
216
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