提交 8276a240 编写于 作者: A Aaron Patterson

fixture methods can be overridden and super() is useful

上级 8c4c07e4
......@@ -874,28 +874,31 @@ def require_fixture_classes(fixture_names = nil)
def setup_fixture_accessors(fixture_names = nil)
fixture_names = Array.wrap(fixture_names || fixture_table_names)
fixture_names.each do |fixture_name|
fixture_name = fixture_name.to_s.tr('./', '_')
methods = Module.new do
fixture_names.each do |fixture_name|
fixture_name = fixture_name.to_s.tr('./', '_')
redefine_method(fixture_name) do |*fixtures|
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
define_method(fixture_name) do |*fixtures|
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
@fixture_cache[fixture_name] ||= {}
@fixture_cache[fixture_name] ||= {}
instances = fixtures.map do |fixture|
@fixture_cache[fixture_name].delete(fixture) if force_reload
instances = fixtures.map do |fixture|
@fixture_cache[fixture_name].delete(fixture) if force_reload
if @loaded_fixtures[fixture_name][fixture.to_s]
@fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find
else
raise StandardError, "No fixture with name '#{fixture}' found for table '#{fixture_name}'"
if @loaded_fixtures[fixture_name][fixture.to_s]
@fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find
else
raise StandardError, "No fixture with name '#{fixture}' found for table '#{fixture_name}'"
end
end
end
instances.size == 1 ? instances.first : instances
instances.size == 1 ? instances.first : instances
end
private fixture_name
end
private fixture_name
end
include methods
end
def uses_transaction(*methods)
......
......@@ -378,6 +378,21 @@ def test_number2
end
end
class OverRideFixtureMethodTest < ActiveRecord::TestCase
fixtures :topics
def topics(name)
topic = super
topic.title = 'omg'
topic
end
def test_fixture_methods_can_be_overridden
x = topics :first
assert_equal 'omg', x.title
end
end
class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
set_fixture_class :funny_jokes => 'Joke'
fixtures :funny_jokes
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册