Hi,
Thanks for this gem. It's very helpful, but I'm running into an issue while testing.
I have 2 associated models:
class Organization
include Mongoid::Document
include Mongoid::Search
field :name
field :description
has_many :locations
search_in :name, :description, :locations => [:name, :description, :keywords]
end
class Location
include Mongoid::Document
field :name
field :description
field :keywords, type: Array
belongs_to :organization
end
Any tests that look for the search term in any of the Location (child) fields fail. Tests that look for the search term in the Organization (parent) name
or description
field work.
Here are my factories:
Organization:
FactoryGirl.define do
factory :organization do
name "Generic Agency"
description "Cooking classes"
end
end
Location:
FactoryGirl.define do
factory :location do
name "Samaritan House"
association :organization, factory: :organization
end
end
The issue is that when you create the location factory, it doesn't automatically save the organization.
loc = FactoryGirl.create(:location)
loc.organization._keywords
# => ["generic", "agency", "cooking", "classes"]
Since the organization's _keywords
does not include the location's info, this test fails:
it "returns locations whose name field includes the search term" do
location = create(:location)
get :search, keyword: "samaritan"
name = response.parsed_body["response"].first["name"]
name.should == "Samaritan House"
end
The only way to make the tests pass is to add an after(:create)
block in the location factory to save the organization:
FactoryGirl.define do
factory :location do
name "Samaritan House"
association :organization, factory: :organization
after(:create) { |loc| loc.organization.save }
end
end