Testing application controller

74 views
Skip to first unread message

Zach

unread,
Jun 24, 2008, 9:29:49 PM6/24/08
to Thinking Sphinx
How do I test ThinkingSphinx? Running an index on the RAILS_ENV=test
makes searchd hang.

Please advise.

Zach

unread,
Jun 24, 2008, 11:28:14 PM6/24/08
to Thinking Sphinx
Meant to say testing search controller.

Pat Allan

unread,
Jun 24, 2008, 11:39:35 PM6/24/08
to thinkin...@googlegroups.com
Ideally you'd just want to stub out your search calls.

But if you'd rather deal with live data, make sure you run the index
and start tasks for the test environment - eg:
rake ts:in RAILS_ENV=test
rake ts:start RAILS_ENV=test

If that doesn't work, let me know and we can try to figure out what's
going wrong.

Cheers

--
Pat

Josh French

unread,
Jun 25, 2008, 8:32:05 AM6/25/08
to Thinking Sphinx
Here's an example of how I test my search actions:

# Controller action:

term = "*#{params[:search]}*"
ThinkingSphinx::Search(term, :with => { :searchable => 1 } })

# RSpec example:

it "should build query" do
ThinkingSphinx::Search.should_receive(:search) do |term,conditions|
term.should eql('*keyword*')
conditions[:with][:searchable].should eql(1)
end
get :show, :search => 'keyword'
end

I stub out the actual search for the same reason that I don't over-
test ActiveRecord validations: your framework and plugins should have
been tested by their creators. This means I can spend more time
testing my own code (which needs it), not my vendors' -- if I thought
a plugin needed more testing, I probably wouldn't be using it :)

j

Zach

unread,
Jul 19, 2008, 5:49:34 PM7/19/08
to Thinking Sphinx
This is an old project and thus is using Rails' inbuilt Testing suite.
So have to look into mocking on it. However, on update requests such
as .destroy and .save it's calling ThinkingSphinx and then crapping
out saying 'unkown index ..'. I don't want it to be calling Sphinx at
all. Is this a configurable option I have not configured?

Thanks
Zach

Pat Allan

unread,
Jul 21, 2008, 10:33:31 PM7/21/08
to thinkin...@googlegroups.com
If you're not mocking/stubbing calls to the database, then Thinking
Sphinx will expect that it can talk to Sphinx as well.

Two approaches: have a Sphinx setup for testing. Which means setting a
different port for the test environment, and running the rake tasks
for that environment as well.

Or: stub the database-reliant calls. You can also disable delta
indexing via ThinkingSphinx.deltas_enabled = false in your test setup
- but some calls to Sphinx aren't reliant on delta indexes.

Cheers

--
Pat

Neil

unread,
Aug 19, 2008, 4:29:12 PM8/19/08
to Thinking Sphinx
Sorry to drag up an old thread; how do I apply Josh's suggestion to
how ThinkingSphinx works now;

# Controller action:

def index
@site_pages = SitePage.search params[:search]
end

# RSpec example:

it "should build query" do
ThinkingSphinx::Search.should_receive(:search) do ?
?
end
get :show, :search => 'keyword'
end

Any suggestions?

Pat Allan

unread,
Aug 20, 2008, 6:55:41 AM8/20/08
to thinkin...@googlegroups.com
Hi Neil

Not quite familiar with this rspec syntax, but try the following:

SitePage.should_receive(:search) do |term|
term.should eql('*keyword*')
end

Cheers

--
Pat
e: p...@freelancing-gods.com || m: +614 1327 3337
w: http://freelancing-gods.com || t: twitter.com/pat
discworld: http://ausdwcon.org || skype: patallan

Neil

unread,
Aug 20, 2008, 2:03:01 PM8/20/08
to Thinking Sphinx
Thanks Pat

This is what I'm seeing right now (I tried your suggestion and many
alternatives, last night);

Mock 'Class' expected :search with (any args) once, but received it 0
times


19 @request.env['HTTP_REFERER'] = "/"
20 SitePage.should_receive(:search) do |term|
21 term.should eql('keyword')
22 end


And here's a bit more of the Rspec syntax, it may help to clear up
what I was trying to do;

describe "handling GET admin/site_pages" do

def do_get
@request.env['HTTP_REFERER'] = "/"
SitePage.should_receive(:search) do |term|
term.should eql('keyword')
end
get :index, :search => 'keyword'
end

it "should be successful" do
do_get
response.should be_success
end

end

Any thoughts?

On Aug 20, 11:55 am, Pat Allan <p...@freelancing-gods.com> wrote:
> Hi Neil
>
> Not quite familiar with thisrspecsyntax, but try the following:
>
> SitePage.should_receive(:search) do |term|
>    term.should eql('*keyword*')
> end
>
> Cheers
>
> --  
> Pat
> e: p...@freelancing-gods.com    || m: +614 1327 3337
> w:http://freelancing-gods.com|| t: twitter.com/pat
> discworld:http://ausdwcon.org|| skype: patallan
>
> On 19/08/2008, at 9:29 PM, Neil wrote:
>
>
>
> > Sorry to drag up an old thread; how do I apply Josh's suggestion to
> > how ThinkingSphinx works now;
>
> > # Controller action:
>
> >  def index
> >    @site_pages = SitePage.search params[:search]
> >  end
>

Pat Allan

unread,
Aug 21, 2008, 5:57:56 AM8/21/08
to thinkin...@googlegroups.com
Hi Neil

I've no idea what the problem is, unfortunately - but I'm not familiar
with the stubbing library you're using.

--
Pat

Reply all
Reply to author
Forward
0 new messages