MiniTest ruby class queries return nil
-
13-07-2021 - |
Question
I am running Rails 3.2.8 and using MinitTest spec for my testing. I have autotest manage my testing suite and everything works great except for one thing. I have a Contacts table that is managed by ActiveRecord with MySQL and a CsvImport custom ruby class that does not have a database table. When I run the application in IRB everything works as expected but when the test is ran through the test suite all the Contact.find / Contact.where / Contact.map query type calls that are in the Ruby class are returned nil.
Here is an example:
require 'test_helper'
class CsvRowManagerTest < MiniTest::Spec
describe 'import tests' do
let(:row) { Hash['name' => 'test'] }
let(:rm) { CsvRowManager.new(row) }
it "should return an array of stuff" do
rm.contacts.wont_be_nil
end
end
end
class CsvRowManager
attr_accessor :row, :contacts
def initialize(row)
@row = row
@contacts = Contact.all.map(&:name) #<-- returns nil
end
end
More of a demonstration than anything. Any ideas?
La solution
Turns out the test database wasn't cloning the data from development! Doh. Thanks for all the help you two; you pushed me in the right direction. @Blowmage even offered to remote debug my problem with me. What a kick ass dude!