https://avo.cool logo
resouce.model coming back nil?
# avo-2
b
We have a system test (using Capybara) for our Avo-backed app that randomly, and infrequently, fails. A little bit of context: we're adding a custom field type for phone numbers that handles formatting (using https://github.com/daddyz/phonelib) to the e164 format before saving them to the DB. The error:
Copy code
ruby
Error:
PhoneNumberFieldTest#test_editing_a_phone_number_that_is_already_properly_formatted:
ActionView::Template::Error: undefined method `has_opted_for_meal_service' for nil:NilClass
    app/avo/resources/client_resource.rb:292:in `block (4 levels) in <class:ClientResource>'
The line throwing the error is for a tab that is conditionally visible. We do that with this code:
Copy code
ruby
    tab "Meal" do
      panel do
        with_options visible: ->(resource:) { resource.model.has_opted_for_meal_service } do
Any ideas what might be causing
resource.model
to occasionally be
nil
? And/or how to shore up test setup/teardown/isolation so that it's more consistent?
m
Thread automatically created by Kyle Adams in #740893011994738751
l
not sure why that flakes out
this is Avo 2, right?
Avo 2 might get into race conditions. it's something we fixed with Avo 3
but please share a gist of the test, maybe I can spot something out
b
Yeah, Avo 2. The gist of the test:
Copy code
ruby
  test "editing a phone number that is already properly formatted" do
    visit "/resources/clients/#{clients(:jane_smith).id}/edit"

    fill_in("Emergency contact phone number", with: "212-300-4000", fill_options: {clear: :backspace})

    click_on "Save"
    click_on clients(:jane_smith).name

    assert_text "EMERGENCY CONTACT PHONE NUMBER\n+1 212-300-4000", count: 1
  end
A race condition feels right here; however, we're not running tests in parallel so it seems like maybe something happening within the app?
l
yes
we were using a badly architected static assignment on a class (example below), which is not threadsafe (currently fixed in Avo 3). that might lead to race conditions in memory example of race condition code https://github.com/avo-hq/avo/blob/2.x/lib/avo/app.rb#L50 https://github.com/avo-hq/avo/blob/2.x/lib/avo/app.rb#L152
I'm not 100% sure this is what you're hitting, but it sounds like something we heard before becuse of our bad architecture
not sure how you could fix that. maybe a tiny
sleep
or something similar? I know it's not the most kosher approach. I would not smile if someone recommended that to me
b
LOL, well, all the more motivation to get to a place where we can upgrade to 3.
I'll see what we can do with a
sleep
.
l
I know. sorry for the extra work 😛
it shouldn't be too much work to upgrade