In browser tests assume state will get out of sync with respect to your test

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the testing category.

Last Updated: 2024-03-28

I had an integration test that created a notes_file instance and then used Selenium to do things to it in the browser. When I ran assertions against the end result, I had weird failures:

let(:note_file) { FactoryBot.create(:notes_file) }

visit admin_notes_pack_path(notes_file.notes_pack)

# Essentially this test is to check if my code for removing
# sensitive information worked.
within("#scrub_text_#{notes_file.subject.id}") do
  fill_in :text, with: 'Private'
  perform_enqueued_jobs do
    click_on 'Remove Text'
  end
end

file = notes_file.save_data_locally(:data)
expect(
  # This function turns the body of a docx file into text
  docx_body(file)
).not_to match(/Private/)

What was up?

The issue was that the notes_file Ruby object was operated on by two processes - Selenium and the one in my test. This caused the state to be out of sync. I needed to force a reload from the DB.

# as before ...

## NEW CODE
notes_file.reload!

# as before ...
file = notes_file.save_data_locally(:data)
expect(
  docx_body(file)
).not_to match(/Private/)