[英]Rails: Integration testing thinking_sphinx with cucumber and webrat - how do I index transactional fixtures?
我想對使用thinking_sphinx & sphinx
Sphinx的搜索功能進行一些Cucumber / Webrat集成測試,但是問題是在典型的黃瓜測試過程中,先加載數據,然后在事務中回滾,所以無法使用thinking_sphinx進行測試。索引它。 或者,是否有一種方法可以僅對部分測試關閉事務?
你解決了這個問題嗎?
[編輯-請不要建議模擬搜索結果。 我希望集成測試可以測試所有功能(包括think_sphinx)的集成。
通過使用數據庫清潔程序插件並按如下所示修改我們的功能/support/env.rb,我們能夠使黃瓜測試成功地與思維獅身人面像配合使用:
# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] ||= "cucumber"
...
# http://github.com/bmabey/database_cleaner
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
Before do
DatabaseCleaner.clean
end
ThinkingSphinx::Configuration.instance.build
ThinkingSphinx::Configuration.instance.controller.start
at_exit do
ThinkingSphinx::Configuration.instance.controller.stop
end
ThinkingSphinx.deltas_enabled = true
ThinkingSphinx.updates_enabled = true
ThinkingSphinx.suppress_delta_output = true
# Re-generate the index before each Scenario
Before do
ThinkingSphinx::Configuration.instance.controller.index
end
如您所見,我們還創建了一個“黃瓜”環境和一個單獨的數據庫(以便同時運行黃瓜和規格,而不會發生沖突)-因此您將需要在數據庫.yml和獅身人面像中添加一個“黃瓜:”條目。 yml,如果您也想這樣做。
我看到問題出在以下語句中:
在典型的黃瓜測試期間,數據已加載並在事務中回滾,因此沒有辦法使think_sphinx對其進行索引
讓think_sphinx索引結果可能不是很快 ,但是在事務中肯定是可能的。 由於這是一次集成測試,並且沒有對您的每個(許多)單元測試都進行過測試,所以我考慮了速度。
因此,現在您需要弄清楚如何在事務處理期間觸發該重新索引。
# somewhere in /features/support:
before('@reindexing') do
require 'Rake'
class MyModel
after_save :force_reindex!
def force_reindex!
# in case multiple runs of this block cause the hook
# to get added multiple times, let's check to make sure
# we haven't already reindexed for this save
return if @__reindexed
Rake["thinking_sphinx:rebuild"].execute
@__reindexed = true
end
end
end
after('@reindexing') do
class MyModel
def force_reindex!
# do nothing; the hook still gets called, but that's ok
end
end
end
在/features/integration.feature
(或其他任何內容)中,
@reindexing
Feature: The whole shebang, altogether
Scenario: ...
鏈接的建議不起作用,因為Rake任務在單獨的過程中調用索引器,因此在測試事務之外也是如此。
除了關閉測試事務以使獅身人面像索引過程可以查看新記錄和更新記錄外,我不知道有什么方法可以解決此問題。 為此,在您的TestCase
,只需添加以下行
self.use_transactional_fixtures = false
請記住,您需要在測試結束時管理清除所有數據。
另一個使用標簽來分隔黃瓜思維獅身人面像測試的示例, 此處使用了黃瓜標簽。
我不建議您測試不屬於您的組件。 這將是一個相當脆弱的測試,因為它將取決於特定版本的獅身人面像中的特定排名算法,該算法稍后可能會更改,因此將來會返回不可預測的結果。
如果要執行此操作,我建議您通過mysql適配器或xmlpipe2選項使用測試數據庫運行單獨的索引進行測試(顯然,xml2pipe允許您更改數據集,而無需更改sphinx文件中的索引選項) 。 這可能需要您一次單獨設置測試數據,刪除索引(使用ruby shell命令),然后強制重新索引(等待索引文檔的總數與已知數據庫記錄的數量相同) ),然后對數據進行測試。
我真的要避免對索引進行測試,就像我說過的那樣,您可能會發現自己不得不面對不斷中斷的測試或緩慢的測試運行時間(取決於要建立索引的數據量)
這可能比它的價值更麻煩,但是您是否嘗試過打開增量索引?
我嘗試了建議的解決方案,但獅身人面像超時並出現以下錯誤。 我認為啟用交易固定裝置后無法重新索引獅身人面像。
錯誤:索引'document_core':sql_query_pre [0]:超出了鎖定等待超時; 嘗試重新啟動事務
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.