【发布时间】:2015-03-07 00:03:23
【问题描述】:
使用 FactoryGirl 创建记录,但该记录类的 search() 方法不返回任何内容。我可以使用 find() 确认该记录确实存在于数据库中。
我发现似乎没有任何效果。已尝试按照标准 thinking_sphinx 和 rspec 文档进行设置。包括我的 rails_helper、spec_helper、spec 和它的输出。非常感谢任何人的指导。(注意:我从这些文件中删除了相当多的内容,以留下足以重现错误的内容)
规格...
require 'rails_helper'
RSpec.describe SiteController do
before(:each) do
@article = create(:article)
end
describe "GET index" do
it "assigns and renders articles" do
get :index
assert_equal assigns(:articles), [@article]
end
end
end
这是输出...
1) SiteController GET index assigns and renders articles
Failure/Error: assert_equal assigns(:articles), [@article]
Minitest::Assertion:
--- expected
+++ actual
@@ -1 +1 @@
-[]
+[#<Article id: 1, title: "Article Title", header_one: "Article Header One", header_two: "Article Header Two", url: "http://www.testurl.com", description: "This is a test article!", body: "This is the article body.", photo_file_name: "test_image.png", photo_content_type: "image/png", photo_file_size: 96884, photo_updated_at: "2015-03-06 23:55:39", delta: true, user_id: 1, category_id: nil, posted_at: nil, created_at: "2015-03-06 23:55:40", updated_at: "2015-03-06 23:55:40">]
rails_helper.rb...
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
config.include FactoryGirl::Syntax::Methods
end
spec_helper.rb...
require 'devise'
module SphinxHelpers
def index
ThinkingSphinx::Test.index
# Wait for Sphinx to finish loading in the new index files.
sleep 0.25 until index_finished?
end
def index_finished?
Dir[Rails.root.join(ThinkingSphinx::Test.config.indices_location, '*.{new,tmp}*')].empty?
end
end
RSpec.configure do |config|
config.raise_errors_for_deprecations!
config.include Devise::TestHelpers, :type => :controller
config.include SphinxHelpers
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
ThinkingSphinx::Test.init
ThinkingSphinx::Test.start_with_autostop
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.before(:each) do |example|
index # if example.metadata[:js]
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
【问题讨论】:
标签: ruby-on-rails-4 rspec thinking-sphinx