Ruby on Rails Fixtures in sub-directories



我正在使用Rails单元测试和夹具框架。除非有我没有见过的配置,否则我不能从子目录中的夹具中使用"高级灯具"(无 id 对象(:

灯具/人物.yml

_fixture:
model_class: Person
myself:
first_name: Me
last_name: Myself

以下调用将按预期传递:

fixtures :people
assert(people(:myself)))

而这个不会(在我将 people.yml 移动到 subdir 之后(:

fixtures "subdir/people"
assert(people(:myself)))

在后一种情况下,我得到的错误是这样的:

NoMethodError: undefined method `people'

使用高级灯具似乎很有价值,但是将我所有的灯具文件都放在/fixtures 的根目录中似乎缺少一些东西。我有几个测试文件,我想让各种测试使用不同的夹具目录。

任何意见将不胜感激。

这是我的最终解决方案:

在夹具文件中,我保留了:

_fixture:
model_class: Person

在测试文件中,我得到:

def people(sym)
subdir_people(sym)
end
def test_myself
assert(people(:myself))
end

这样,它使我免于重构我的大量 people(( 调用。或者,如果我愿意,我可以使用 subdir_people((。

我会尝试在/fixtures/subdir/people.yml中保存people.yml。 文档在这里 https://apidock.com/rails/ActiveRecord/TestFixtures/ClassMethods/set_fixture_class .

test_helper.rb
set_fixture_class :people => 'Subdir::People'
in your test file.
before do
myself = subdir_people(:myself)
register(myself)
end
it "should test myself" do 
assert(people(:myself)))
end

最新更新