Search code examples
postgresqlactiverecordrspecrspec-railsruby-on-rails-6

Rspec DB View Is Empty


I created a LeadIndex view within my Rails application that is based on my Lead model. When running my test suite (Rspec) the table (db view) is recognized but returns as an empty [] of LeadIndex::ActiveRecord_Relation. The Lead model is showing that there are objects stored within the database, but the view doesn't seem to pick it up.

For example:

Lead.all =>

[#<Lead:0x000000010a70a9a8
  id: 850,
  source: "Web",
  user_id: nil,
  created_at: Tue, 12 Oct 2021 18:48:32.505988000 UTC +00:00,
  updated_at: Tue, 12 Oct 2021 18:48:32.521121000 UTC +00:00,
  received_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
  assigned_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
  agency_id: 452,
  person_id: 882,
  age: 41,
  status: "prospecting",
  dnc: false,
  last_contact_time: nil>]
LeadIndex.all  => [] # actual

LeadIndex.all => #expected
[#<LeadIndex:0x000000015221ed48
 id: 850,
 source: "Web",
 user_id: nil,
 created_at: Tue, 12 Oct 2021 18:48:32.505988000 UTC +00:00,
 updated_at: Tue, 12 Oct 2021 18:48:32.521121000 UTC +00:00,
 received_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 assigned_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 agency_id: 452,
 person_id: 882,
 age: 0,
 status: "prospecting",
 dnc: false,
 last_contact_time: nil,
 scheduled_time: nil,
 name: "John Doe",
 person_state: "TX",
 number: "(555) 555-5555",
 num_outbound_dials: 3,
 last_activity: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 agent_name: "Tom Cruise",
 lang: "English",
 next_dial_time: "morning">
irb(main):002:0>]

In development mode everything works perfectly.


Solution

  • Well I am a knucklehead. I figured it out for me. In my SQL query I hard a where clause to bring up results only with an ID that matches my development DB object.

    So when when the test environment made up random IDs for their objects the WHERE clause was filtering out all of them because there was no ID with the exact number that matched my development DB id..

    a big d'oh! Lessons learned.