I've written some RSpec tests that successfully create objects with :let statements. However, the test environment doesn't maintain the associations that function properly everywhere else. Below is an example of a class that would turn up a NoMethodError (undefined method `money' for nil:NilClass). Money is a column in Inventory. Any thoughts?
class Inventory < ActiveRecord::Base
describe 'Test methods' do
it "test method" do
There is no reason this shouldn't work. RSpec isn't special, it's just regular Ruby code. You can confirm this by moving all of your code into a single file, something like:
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') ActiveRecord::Base.logger = Logger.new(STDOUT) class Inventory < ActiveRecord::Base end class Character < ActiveRecord::Base has_one :inventory end describe 'test' do it 'works' do puts Character.first.inventory.money.inspect end end
Guesses as to what may be broken:
moneyis a composite field or something like that. Can you post your database schema?
puts $LOADED_FEATURESto verify that all the files that should be required have been.