Thanks Wolas!
Sorry if this seems like a newbie question, what am I supposed replace
the stubbed_method_name with?
Also, I came across the only thread on the Interwebs which seems to
cover this topic - it''s over a year old, but I think they have a
solution which could work around the issues you pointed out. However, I
couldn''t work out how to get their subbing approach to work with the
way
the ThinkingSphinx syntax looks right now...
Back in the day, they were doing this;
# 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
But I''m on the latest version of the plugin, and it looks like this;
# 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 ideas?
http://groups.google.com/group/thinking-sphinx/browse_thread/thread/b879c24ecf06fb12/167f1a92e7d27a72?lnk=gst&q=rspec#167f1a92e7d27a72
"Wolas!" wrote:> in spec helper, overwrite the class that you want to stub
> (ThinkingSphinx::ActiveRecord::Search) and stub the method itself.
>
> module ThinkingSphinx::ActiveRecord::Search
>
> def stubbed_method-name
> # do nothing
> end
> end
>
>
> put it in the spec helper. as this file is loaded before any spec run
> (of any file or batch) it will have been the last one to be loaded and
> hence will do nothing, but still run your specs.
>
>
> notice you cannot test any method that uses this module since you have
> overridden it completely. so it will help if you want to check the
> view renders or your controller does the right thing, but you will not
> be able to test the ThinkingSphinx functionallity at all.
>
> On Aug 19, 8:57�pm, Neil Cauldwell <rails-mailing-l...@andreas-s.net>
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
The mocking infrastructure in Rspec-rails is very simple. there are
two ways to "stub" methods:
object.should_receive(:method_name).and_return(whatever)
this is an assertion. it means that the method_name methods HAS to be
called at least once for this test to pass. Also note that the method
itself is never called, so if youre having troubles, just put this
line with the ThinkingSphinx method that is giving you troubles (the
method name you had to replace on my first reply. an example
it ''should look up the money in the account'' do
@account.should_receive(:money).once # this is our test
@account.balance # here we call
the method that i am testing
end
===class Account
def balance
self.money.to_f
end
end
in the above example, the "money" method will not actually be called.
the other way is to stub, which means that you are not testing for the
method call, you just want to control the method within a test:
it ''should look up the money in the account'' do
@account.stub!(:money).and_return(98) # money method will always
return 98
@account.balance.should eql(98) # the test which will
pass
end
in this example the money method is never being called, the testing
framework takes turn and runs its stub.
As a note, you can stub anything, classes instances, arrays, ,..
anything that is an object which in ruby is everything
maybe it doesnt help maybe it does...
good luck
On Aug 20, 7:05 pm, Neil Cauldwell
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Thanks Wolas!
>
> Sorry if this seems like a newbie question, what am I supposed replace
> the stubbed_method_name with?
>
> Also, I came across the only thread on the Interwebs which seems to
> cover this topic - it''s over a year old, but I think they have a
> solution which could work around the issues you pointed out. However, I
> couldn''t work out how to get their subbing approach to work with
the way
> the ThinkingSphinx syntax looks right now...
>
> Back in the day, they were doing this;
>
> # 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
>
> But I''m on the latest version of the plugin, and it looks like
this;
>
> # 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 ideas?
>
> http://groups.google.com/group/thinking-sphinx/browse_thread/thread/b...
>
>
>
> "Wolas!" wrote:
> > in spec helper, overwrite the class that you want to stub
> > (ThinkingSphinx::ActiveRecord::Search) and stub the method itself.
>
> > module ThinkingSphinx::ActiveRecord::Search
>
> > def stubbed_method-name
> > # do nothing
> > end
> > end
>
> > put it in the spec helper. as this file is loaded before any spec run
> > (of any file or batch) it will have been the last one to be loaded and
> > hence will do nothing, but still run your specs.
>
> > notice you cannot test any method that uses this module since you have
> > overridden it completely. so it will help if you want to check the
> > view renders or your controller does the right thing, but you will not
> > be able to test the ThinkingSphinx functionallity at all.
>
> > On Aug 19, 8:57 pm, Neil Cauldwell
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
"Wolas!" wrote:> The mocking infrastructure in Rspec-rails is very simple. there are > two ways to "stub" methods: > > object.should_receive(:method_name).and_return(whatever) > > this is an assertion. it means that the method_name methods HAS to be > called at least once for this test to pass. Also note that the method > itself is never called, so if youre having troubles, just put this > line with the ThinkingSphinx method that is giving you troubles (the > method name you had to replace on my first reply. an example > > it ''should look up the money in the account'' do > @account.should_receive(:money).once # this is our test > @account.balance # here we call > the method that i am testing > end > > ===> class Account > def balance > self.money.to_f > end > end > > > in the above example, the "money" method will not actually be called. > > > the other way is to stub, which means that you are not testing for the > method call, you just want to control the method within a test: > > it ''should look up the money in the account'' do > @account.stub!(:money).and_return(98) # money method will always > return 98 > @account.balance.should eql(98) # the test which will > pass > end > > in this example the money method is never being called, the testing > framework takes turn and runs its stub. > > As a note, you can stub anything, classes instances, arrays, ,.. > anything that is an object which in ruby is everything > > maybe it doesnt help maybe it does... > > good luck > > On Aug 20, 7:05�pm, Neil Cauldwell <rails-mailing-l...@andreas-s.net>Thanks again. You''re too cool. I''m doing this (and it works for me and my standards); def do_get ThinkingSphinx::Search.stub!(:search).and_return(users) get :index, :search => "bob" end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---