1

有没有人有一个很好的例子,将 RSpec 与 sunspot_matchers gem(或只是普通的 RSpec)一起使用来测试使用 sunspot gem 的代码?

我试图弄清楚这个错误是怎么回事,但找不到任何可靠的例子。

以下是我目前正在处理的模型、规范和规范输出的片段:

/models/channel.rb

class Channel < ActiveRecord::Base

  #...some attributes and methods here

  private

  # selected_facets is a hash with strings as keys
  # facet_categories is an array of symbols
  def self.search_results(search_query, selected_facets, facet_categories)
    Channel.search do
      keywords(search_query)
      facet_filter = selected_facets

      dynamic :facets do
        facet_categories.each do |category_name|
          if selected_facets.nil? or selected_facets[category_name.to_s].nil?
            facet(category_name)
          else
            with(category_name, selected_facets[category_name.to_s])
          end
        end
      end

    end
  end
end

/spec/models/channel_spec.rb

describe ".search_results" do
  let(:facet_categories) { [:category1, :category2, :category3] }
  let(:query) { "just a test query" }

  it "should search for Channels" do
   selected_facets = ""
   Channel.search_results(query, selected_facets, facet_categories)
   Sunspot.session.should be_a_search_for(Channel)
  end

  context "when there are no selected facets" do
    let(:selected_facets) { nil }
    it "it facets on that category name" do
      channels = Channel.search_results(query, selected_facets, facet_categories)
      Sunspot.session.should have_search_params(:facet, :category1)
    end
  end

  context "when there are no selected facets for the given category name" do
    it "it facets on the given category name"
  end
end

RSpec 输出,带有 (r)、(y)、(g) 来显示失败、未决和通过的规范

Channel
  #to_param
    (g)parameterizes id and name
  #remove_phone_numbers_from_tooltip_terms
    (g)removes phone numbers and trailing spaces from tooltip_terms
  #remove_characters_from_tooltip_terms
    (g)removes non-alphanumeric characters and trailing spaces from tooltip_terms
  .search_results
    (g)should search for Channels
    when there are no selected facets
      (r)it facets on that category name (FAILED - 1)
    when there are no selected facets for the given category name
      (y)it facets on the given category name (PENDING: Not Yet Implemented)

Pending:

Channel.search_results when there are no selected facets for the given category name   it facets on the given category name (Not Yet Implemented)
./spec/models/channel_spec.rb:53

1)
Sunspot::UnrecognizedFieldError in 'Channel.search_results when there are no selected facets it facets on that category name'
No field configured for Channel with name 'category1' ./spec/models/channel_spec.rb:48:

Finished in 0.191886 seconds

6 examples, 1 failure, 1 pending

我正在尝试在上面显示的模型中测试 if/else 条件。

有任何想法吗?或者对整体方法的建议?

一些好的例子/文档会很棒......或者我可能只是在某个地方错过了一些东西。

谢谢,

肯顿

4

0 回答 0