【发布时间】:2017-02-03 18:13:58
【问题描述】:
我想使用 FactoryGirl 来构建模型的内存存根,然后让所有 ActiveRecord 查询仅针对那些运行。例如:
# Assume we start with an empty database, a Foo model,
# and a Foo factory definition.
#foo_spec.rb
stubbed_foo = FactoryGirl.build_stubbed(:foo)
# Elsewhere, deep in the guts of application
Foo.first() # Ideally would return the stubbed_foo we created
# in the test. Currently this returns nil.
解决方案可能是使用内存数据库。但是上述情况可能吗?
【问题讨论】:
-
如果您没有任何表,那么模型就没有要遵循的架构,因此您的存根模型可以像任何对象一样运行,而不必与生产中存在的实际架构匹配。这可能是个坏主意。你为什么要以这种方式绕过 ActiveRecord?
-
你可以用灯具代替工厂女孩
-
@trueinViso 我不认为fixtures能解决我的问题,因为Foo类在调用
first()或任何其他查询方法时仍然会进入数据库。
标签: rails-activerecord factory-bot ruby-on-rails-5 rspec3 rspec-mocks