开发者

Writing RSpec specs to test app using sunspot_rails

开发者 https://www.devze.com 2023-03-22 10:55 出处:网络
Does anyone have a good example of using RSpec with the sunspot_matchers gem (or just plain RSpec) to test code using the sunspot gem?

Does anyone have a good example of using RSpec with the sunspot_matchers gem (or just plain RSpec) to test code using the sunspot gem?

I'm trying to 开发者_如何学Gofigure out what's going on with this error and can't find any solid examples.

Here are snippets from the model, spec and spec output I'm currently working on:

/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 output, with (r), (y), (g) to show failing, pending and passing specs

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

I'm trying to test the if/else conditional in the model shown above.

Any ideas? Or advice on the approach overall?

Some good examples / documentation would be awesome...or maybe I just missed something somewhere.

Thanks,

Kenton

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号