Fixtures using csv

Hi there,

I got a Problem using comma-seperated-values as fixtures. It simply does
not fixture anything, no error message at all. :frowning:

Here my code (… => RAILS_ROOT):

the test in …/test/unit/uk/incode_test.rb

require File.dirname(FILE) + ‘/…/…/test_helper’

class UK::IncodeTest < Test::Unit::TestCase
fixtures File.join( ‘uk’, ‘uk_incodes’ )

def test_class_for
# Returns record for provided incode.code and caches set
time_before = Time.now
assert_nil UK::Incode.for(‘0000’)
distance = Time.now - time_before

time_before = Time.now
assert_instance_of UK::Incode, UK::Incode.for('PH50')

# stupid test...
assert distance > Time.now - time_before

end

end

the csv in …/test/fixtures/uk/uk_incodes.csv

code, posttown, county
AB10, ABERDEEN, Aberdeenshire
AB11, ABERDEEN, Aberdeenshire
AB12, ABERDEEN, Aberdeenshire
AB13, MILLTIMBER, Aberdeenshire
AB14, PETERCULTER, Aberdeenshire
AB15, ABERDEEN, Aberdeenshire

snip

the model in …/app/models/uk/incode.rb

module UK
class Incode < ActiveRecord::Base
set_table_name ‘uk_incodes’

validates_presence_of :code
validates_presence_of :county

@@postcodes = Hash.new

# Returns record for provided incode.code and caches set
def self.for code
  if @@postcodes.empty?
    self.find( :all ).each do |postcode|
      @@postcodes[ postcode.code.to_sym ] = postcode
    end
  end
  p @@postcodes

  @@postcodes[ code.to_sym ]
end

end
end

Thanks for any advice
Florian

Florian Aßmann wrote:

Hi there,

I got a Problem using comma-seperated-values as fixtures. It simply does
not fixture anything, no error message at all. :frowning:
[snip]
class UK::IncodeTest < Test::Unit::TestCase
fixtures File.join( ‘uk’, ‘uk_incodes’ )

This fixtures call does not seem like it is correct. At least not from
what I can gather from the API docs. Have you tried reading:

http://api.rubyonrails.com/classes/Fixtures.html


Cheers,

  • Jacob A.

Hi,

I tried to reproduce your problem but it works perfectly in my
environment. Maybe you should check that there is no uk_incodes.yml file
in the same directory than the CSV fixtures file?

Simon

Florian Aßmann wrote:

parameter because I put the fixture into a subdir it worked.

Maybe some update in Rails Fixtures or so… :frowning:

Just for the record, what version of Rails are you using? My first test
was with Rails 1.2.3. I have double checked with Rails Edge (revision
7071) and it’s still working…

Simon

Hi Simon,

I’m using Version 1.2.3 too.

Regards
Florian

Hi Simon

I tried to reproduce your problem but it works perfectly in my
environment. Maybe you should check that there is no uk_incodes.yml file
in the same directory than the CSV fixtures file?

No, there isn’t. I moved uk/uk_incodes.csv to uk_incodes.csv and used
the syntax ‘fixtures :uk_incodes’ (thanks for the hint Jacob) and it
worked right out of the box. The last time I used a String as fixtures
parameter because I put the fixture into a subdir it worked.

Maybe some update in Rails Fixtures or so… :frowning:

Thanks
Florian