Permalink
Browse files

Using RSpec's stubbing, to simplify testing, instead of NotAMock *and…

…* RSpec stubs.
  • Loading branch information...
1 parent e41ac63 commit 74830dafb47ac7fa5f0272f2f5576e8e8de0d123 @pat pat committed Sep 17, 2009
@@ -3,26 +3,22 @@
describe "ThinkingSphinx::ActiveRecord::Delta" do
it "should call the toggle_delta method after a save" do
@beta = Beta.new(:name => 'beta')
- @beta.stub_method(:toggle_delta => true)
+ @beta.should_receive(:toggle_delta).and_return(true)
@beta.save
-
- @beta.should have_received(:toggle_delta)
end
it "should call the toggle_delta method after a save!" do
@beta = Beta.new(:name => 'beta')
- @beta.stub_method(:toggle_delta => true)
+ @beta.should_receive(:toggle_delta).and_return(true)
@beta.save!
-
- @beta.should have_received(:toggle_delta)
end
describe "suspended_delta method" do
before :each do
- ThinkingSphinx.stub_method(:deltas_enabled? => true)
- Person.sphinx_indexes.first.delta_object.stub_method(:` => "")
+ ThinkingSphinx.deltas_enabled = true
+ Person.sphinx_indexes.first.delta_object.stub!(:` => "")
end
it "should execute the argument block with deltas disabled" do
@@ -34,13 +30,13 @@
end
it "should restore deltas_enabled to its original setting" do
- ThinkingSphinx.stub_method(:deltas_enabled? => false)
+ ThinkingSphinx.deltas_enabled = false
ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
Person.suspended_delta { 'no-op' }
end
it "should restore deltas_enabled to its original setting even if there was an exception" do
- ThinkingSphinx.stub_method(:deltas_enabled? => false)
+ ThinkingSphinx.deltas_enabled = false
ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
lambda { Person.suspended_delta { raise 'bad error' } }.should(
raise_error(Exception)
@@ -70,54 +66,52 @@
describe "index_delta method" do
before :each do
- ThinkingSphinx::Configuration.stub_method(:environment => "spec")
- ThinkingSphinx.stub_method(:deltas_enabled? => true, :sphinx_running? => true)
- Person.delta_object.stub_methods(:` => "", :toggled => true)
+ ThinkingSphinx::Configuration.stub!(:environment => "spec")
+ ThinkingSphinx.deltas_enabled = true
+ ThinkingSphinx.updates_enabled = true
+ ThinkingSphinx.stub!(:sphinx_running? => true)
+ Person.delta_object.stub!(:` => "", :toggled => true)
@person = Person.new
- @person.stub_method(
+ @person.stub!(
:in_both_indexes? => false,
:sphinx_document_id => 1
)
@client = Riddle::Client.new
@client.stub!(:update => true)
- Riddle::Client.stub_method(:new => @client)
+ Riddle::Client.stub!(:new => @client)
end
it "shouldn't index if delta indexing is disabled" do
- ThinkingSphinx.stub_method(:deltas_enabled? => false)
+ ThinkingSphinx.deltas_enabled = false
+ Person.sphinx_indexes.first.delta_object.should_not_receive(:`)
+ @client.should_not_receive(:update)
@person.send(:index_delta)
-
- Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
- @client.should_not have_received(:update)
end
it "shouldn't index if index updating is disabled" do
- ThinkingSphinx.stub_method(:updates_enabled? => false)
+ ThinkingSphinx.updates_enabled = false
+ Person.sphinx_indexes.first.delta_object.should_not_receive(:`)
@person.send(:index_delta)
-
- Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
end
it "shouldn't index if the environment is 'test'" do
- ThinkingSphinx.unstub_method(:deltas_enabled?)
ThinkingSphinx.deltas_enabled = nil
- ThinkingSphinx::Configuration.stub_method(:environment => "test")
+ ThinkingSphinx::Configuration.stub!(:environment => "test")
+ Person.sphinx_indexes.first.delta_object.should_not_receive(:`)
@person.send(:index_delta)
-
- Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
end
it "should call indexer for the delta index" do
- @person.send(:index_delta)
-
- Person.sphinx_indexes.first.delta_object.should have_received(:`).with(
+ Person.sphinx_indexes.first.delta_object.should_receive(:`).with(
"#{ThinkingSphinx::Configuration.instance.bin_path}indexer --config #{ThinkingSphinx::Configuration.instance.config_file} --rotate person_delta"
)
+
+ @person.send(:index_delta)
end
it "shouldn't update the deleted attribute if not in the index" do
@@ -127,7 +121,7 @@
end
it "should update the deleted attribute if in the core index" do
- @person.stub_method(:in_both_indexes? => true)
+ @person.stub!(:in_both_indexes? => true)
@client.should_receive(:update)
@person.send(:index_delta)
@@ -3,51 +3,47 @@
describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
describe "search method" do
before :each do
- Friendship.stub_method(:search => true)
+ Friendship.stub!(:search => true)
@person = Person.find(:first)
@index = Friendship.sphinx_indexes.first
end
it "should raise an error if the required attribute doesn't exist" do
- @index.stub_method(:attributes => [])
+ @index.stub!(:attributes => [])
lambda { @person.friendships.search "test" }.should raise_error(RuntimeError)
-
- @index.unstub_method(:attributes)
end
it "should add a filter for the attribute into a normal search call" do
- @person.friendships.search "test"
+ Friendship.should_receive(:search) do |query, options|
+ options[:with][:person_id].should == @person.id
+ end
- Friendship.should have_received(:search).with(
- "test", :with => {:person_id => @person.id}
- )
+ @person.friendships.search "test"
end
end
describe "search method for has_many :through" do
before :each do
- Person.stub_method(:search => true)
+ Person.stub!(:search => true)
@person = Person.find(:first)
@index = Person.sphinx_indexes.first
end
it "should raise an error if the required attribute doesn't exist" do
- @index.stub_method(:attributes => [])
+ @index.stub!(:attributes => [])
lambda { @person.friends.search "test" }.should raise_error(RuntimeError)
-
- @index.unstub_method(:attributes)
end
it "should add a filter for the attribute into a normal search call" do
- @person.friends.search "test"
+ Person.should_receive(:search).with do |query, options|
+ options[:with][:friendly_ids].should == @person.id
+ end
- Person.should have_received(:search).with(
- "test", :with => {:friendly_ids => @person.id}
- )
+ @person.friends.search "test"
end
end
end
Oops, something went wrong.

0 comments on commit 74830da

Please sign in to comment.