如何使用 RSpec 测试 ThinkingSphinx [英] How to test ThinkingSphinx using RSpec
问题描述
我在一个模型中有一个类方法,它调用了 thinking_sphinx 的 search() 方法.我需要检查这个类方法.
I have a class method in a model that calls thinking_sphinx's search() method. I need to check this class method.
我想在我的 rspec 测试用例中启动、索引或停止 sphinx.我正在尝试使用这段代码.
I want to start, index or stop sphinx in my rspec test cases. I am trying with this piece of code.
before(:all) do
ThinkingSphinx::Test.start
end
after(:all) do
ThinkingSphinx::Test.stop
end
并在我触发搜索查询之前在每个测试用例中使用此代码
and with this code in each test case before I fire the search query
ThinkingSphinx::Test.index
但是在我触发搜索查询之后,它给了我空的结果,尽管测试数据库中有完全匹配.
but still after I fire the search query, it gives me empty results though exact matches are there in the test db.
如果您将 rspec 与 thinking_sphinx 一起使用,请指导我提供代码示例
Please guide me with code examples if you are using rspec with thinking_sphinx
推荐答案
按照 David 的帖子,我们最终得到以下解决方案:
Following David post, we end up with following solution:
#spec/support/sphinx_environment.rb
require 'thinking_sphinx/test'
def sphinx_environment(*tables, &block)
obj = self
begin
before(:all) do
obj.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation, {:only => tables}
ThinkingSphinx::Test.create_indexes_folder
ThinkingSphinx::Test.start
end
before(:each) do
DatabaseCleaner.start
end
after(:each) do
DatabaseCleaner.clean
end
yield
ensure
after(:all) do
ThinkingSphinx::Test.stop
DatabaseCleaner.strategy = :transaction
obj.use_transactional_fixtures = true
end
end
end
#Test
require 'spec_helper'
require 'support/sphinx_environment'
describe "Super Mega Test" do
sphinx_environment :users do
it "Should dance" do
ThinkingSphinx::Test.index
User.last.should be_happy
end
end
end
它将指定的表切换到 :truncation 策略,然后将它们切换回 :trasaction 策略.
It switch specified tables to :truncation strategy, and after that switch them back to :trasaction strategy.
这篇关于如何使用 RSpec 测试 ThinkingSphinx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!