[rspec-users] Cucumber vs Rails Fixtures

Wolfram Arnold lists at ruby-forum.com
Fri Jun 5 18:27:13 EDT 2009


The method I posted last week only works for Cucumber prior to 0.2.3.2.

For 0.2.3.2 and later, you cannot pass a block to the World more than 
once, and thus the new way would be (in env.rb):

module FixtureAccess

  def self.included(base)
    (class << base; self; end).class_eval do
      @@fixture_cache = {}
      fixtures.each do |table_name|
        table_name = table_name.to_s.tr('.', '_')
        define_method(table_name) do |*fixture_symbols|
          @@fixture_cache[table_name] ||= {}

          instances = fixture_symbols.map do |fixture_symbol|
            if fix = 
Fixtures.cached_fixtures(ActiveRecord::Base.connection, 
table_name)[fixture_symbol.to_s]
              @@fixture_cache[table_name][fixture_symbol] ||= fix.find 
# find model.find's the instance
            else
              raise StandardError, "No fixture with name 
'#{fixture_symbol}' found for table '#{table_name}'"
            end
          end
          instances.size == 1 ? instances.first : instances
        end
      end
    end
  end

end

... then ...

World(FixtureAccess)

-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list