Skip to content

Instantly share code, notes, and snippets.

@batasrki
Created September 26, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batasrki/1243788 to your computer and use it in GitHub Desktop.
Save batasrki/1243788 to your computer and use it in GitHub Desktop.
Example model search spec
describe Job do
before :each do
@mock_client = mock_model(Client, {:name => "Test Client"})
@job = Job.new(:name => "test job", :client => @mock_client,
:category => @mock_job_category)
end
describe "search" do
before :each do
@category = JobCategory.create!(:name => "Testing search")
end
it "should search for a term without a join" do
@job.save
Job.search("jobs.name like '%test%'", 1, "client_id in (#{@mock_client.id})", nil).should include(@job)
end
it "should search for a term without a join and a condition" do
@job.save
Job.search("jobs.name like '%test%'", 1, nil, nil).should include(@job)
end
it "should search for a term with a join" do
@job.category = @category
@job.save
Job.search("jobs.name like '%test%'", 1, "client_id in (#{@mock_client.id})", :category).should include(@job)
end
it "should return the right order of jobs when searching for a term with a join and condition" do
@job.category = @category
@job.save
Job.create!(:name =>"Before the other test one", :client => @mock_client, :category => @category, :users => [@mock_member])
found = Job.search("jobs.name like '%test%'", 1, "client_id in (#{@mock_client.id})", :category)
found.size.should == 2
found.first.name.should match(/^Before/)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment