编写RSpec规范以使用sunspot_rails测试应用程序

时间:2011-07-22 13:34:54

标签: ruby-on-rails rspec sunspot-rails

有没有人有一个很好的例子,使用sunspot_matchers gem(或只是普通的RSpec)使用RSpec来测试使用太阳黑子宝石的代码?

我正在试图弄清楚这个错误发生了什么,但不能 找到任何可靠的例子。

以下是我正在处理的模型,规格和规格输出的片段:

/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条件 上方。

有什么想法吗?或整体方法的建议?

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

谢谢,

肯通

0 个答案:

没有答案