I am experiencing a head-scratcher in a non-Rails app using AR that I cannot figure out. I will simplify greatly, but here is the essence: I have a Download object that belongs to a Ledger. In one of my unit tests, I am experiencing this:
dl = create(:download, account: checking)
dl.ledger
=> <Byr::Ledger:0x00007fd176ce4740
id: 2,
name: "test_ledger_1",
org_name: "Test Ledger, Inc.",
street1: nil,
street2: nil,
city: nil,
state: nil,
zip: nil,
created_at: 2022-04-03 13:13:53.734003153 UTC,
updated_at: 2022-04-03 13:13:53.792451592 UTC,
default_account_id: 21,
short_name: "Test_ledger_1",
parent_percent: nil,
parent_id: nil,
accessed_at: 2022-04-03 13:13:53.791911547 UTC,
start_date: Mon, 01 Jan 2018,
cost_method: "fifo",
end_date: nil
> dl.ledger.peristed? => true
> Ledger.find(2) => nil with eval error: Couldn't find Byr::Ledger with 'id'=2
I use factory_bot to create the Download, dl, which in turn builds a ledger for it to go with, which purports to be persisted with id=2. But when I try to find the ledger with Ledger.find(2), it's not in the postgresql db.
Anybody have any idea what might be going on here?
CodePudding user response:
I think you should use something like faker factory_bot and mock resources to get access to params you need. Here an example of use. Hope this helps
CodePudding user response:
I finally got this to pass by explicityly passing the ledger into the Account factory on which the Download factory was created, like this:
let(:ldg) { create(:ledger, name: 'Willy') }
let(:checking) { create(:bank_account, ledger: ldg) }
let(:dl) { create(:download, account: checking) }
Suddenly, the disappearing ledgers abated. I believe the cuplrit may have been DatabaseCleaner wiping away the automatically-generated ledger from one example to the next. Using an explicit ledger just for the example in question kept it from being deleted underneath my feet.
Thanks, @Joe, for giving this some thought.