Created
August 19, 2013 17:10
-
-
Save davetapley/6271535 to your computer and use it in GitHub Desktop.
Failure to run counter_culture callbacks, when under rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):050:0> user = User.find 1 | |
[snip] | |
irb(main):051:0> events = FactoryGirl.create_list :place_event, 3, user: user | |
[snip] | |
irb(main):052:0> new_place = FactoryGirl.create :place, user: user | |
[snip] | |
irb(main):067:0> PlaceEvent.where("id IN (#{ events.collect(&:id).join(',') })").move_to new_place | |
[snip] | |
SQL (1.1ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) + 1 WHERE "places"."id" = 1799211 | |
SQL (1.0ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) - 1 WHERE "places"."id" = 1799208 | |
SQL (1.1ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) + 1 WHERE "places"."id" = 1799211 | |
SQL (1.0ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) - 1 WHERE "places"."id" = 1799209 | |
SQL (1.1ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) + 1 WHERE "places"."id" = 1799211 | |
SQL (1.2ms) UPDATE "places" SET "place_events_count" = COALESCE("place_events_count", 0) - 1 WHERE "places"."id" = 1799210 | |
[snip] | |
irb(main):069:0* events.map(&:place).map(&:reload).map(&:place_events_count) | |
Place Load (0.9ms) SELECT "places".* FROM "places" WHERE "places"."id" = $1 LIMIT 1 [["id", 1799208]] | |
Place Load (0.6ms) SELECT "places".* FROM "places" WHERE "places"."id" = $1 LIMIT 1 [["id", 1799209]] | |
Place Load (0.6ms) SELECT "places".* FROM "places" WHERE "places"."id" = $1 LIMIT 1 [["id", 1799210]] | |
=> [0, 0, 0] | |
irb(main):070:0> new_place.reload.place_events_count | |
Place Load (0.7ms) SELECT "places".* FROM "places" WHERE "places"."id" = $1 LIMIT 1 [["id", 1799211]] | |
=> 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlaceEvent < Event | |
belongs_to :place | |
counter_culture :place | |
[snip] | |
def self.move_to(place) | |
PlaceEvent.transaction do | |
scoped.each do |e| | |
e.update_attribute :place, place | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe PlaceEvent do | |
let(:user) { FactoryGirl.create :user } | |
[snip] | |
describe '.move_to' do | |
let!(:events) { FactoryGirl.create_list :place_event, 3, user: user } | |
let!(:event_places) { events.map &:place } | |
let!(:new_place) { FactoryGirl.create :place, user: user } | |
before do | |
PlaceEvent.scoped.move_to new_place | |
end | |
it 'moves all events to a new place' do | |
events.each do |e| | |
PlaceEvent.find(e.id).place.should == new_place | |
end | |
end | |
it 'updates the counter cache on all old places' do | |
event_places.each do |p| | |
Place.find(p.id).place_events.size.should == 0 | |
end | |
end | |
it "updates the new place's counter cache" do | |
Place.find(new_place.id).place_events_count.should == event_places.size | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) PlaceEvent.move_to updates the new place's counter cache | |
Failure/Error: Place.find(new_place.id).place_events_count.should == event_places.size | |
expected: 3 | |
got: 0 (using ==) | |
# ./spec/models/place_event_spec.rb:69:in `block (3 levels) in <top (required)>' | |
Tailing log/test.log and grepping for the COALESCE call yields nothing, otherwise the log looks correct, including addition callbacks defined in PlaceEvent being called. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment