ruby - Configuring fixture_path in ActiveRecord test fixtures -
i'm using activerecord sinatra instead of rails, , want use fixtures in tests. documentation activerecord's fixtureset says have use fixture_path tell fixture files are:
placed in directory appointed activesupport::testcase.fixture_path=(path)
how can write setting? tried @fixture_path , @@fixture_path, both of them left value nil when fixtureset tried read it.
here's thing work, can't possibly right:
# test_helper.rb require_relative '../app' require 'minitest/autorun' require 'active_record' activerecord::base.establish_connection(:test) #set fixtures , such class activesupport::testcase include activerecord::testfixtures include activerecord::testfixtures::classmethods class << self def fixtures(*fixture_set_names) self.fixture_path = 'test/fixtures' super *fixture_set_names end end self.use_transactional_fixtures = true self.use_instantiated_fixtures = false end the full source code posted small demo project activerecord , sinatra.
i tried leave comment on answer, got long thought might put in answer.
the reason @fixture_path , @@fixture_path didn't work fixture_path activesupport class attribute, ruby attr_accessor except it's defined singleton method on class. can see fixture_path attribute defined class_attribute :fixture_path in activerecord::testfixtures module source.
class attributes part of activesupport , not native ruby. can read more them in active support core extensions rails guide , in api docs, , see how class_attribute works in rails source. can see, value stored in instance variable "@#{name}" (e.g. @fixture_path), happens inside singleton method, means it's instance variable on singleton class , can access within singleton class.
that's little bit moot, though, because point of attributes (and feel free disregard if it's old news you) allow keep instance variables private , change implementation without breaking code subclasses or includes code. when attribute reader or writer exists, should use instead of accessing instance variable directly, because @ point implementation might change , attribute methods replaced methods more complex logic, , accessing instance variable directly no longer produce same results using attribute reader , writer.
as discovered, need use self.fixture_path = instead of fixture_path = because in latter case ruby assumes want assign local variable.
Comments
Post a Comment