我想使用FactoryGirl構建模型的內存中存根,然後讓所有ActiveRecord查詢僅針對這些查詢運行。例如:如何完全避免在RSpec測試中使用數據庫?
# 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
@trueinViso我不認爲燈具會解決我的問題,因爲在調用'first()'或任何其他查詢方法時,Foo類仍然會去數據庫。 –