From ef98b601848bda46c7517d983e904659875f99d7 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 14 May 2013 11:59:55 +1000 Subject: [PATCH] disable observers in tests, enable as needed, tests are 20% faster --- spec/components/jobs/exporter_spec.rb | 4 +- spec/components/post_creator_spec.rb | 4 ++ spec/components/post_destroyer_spec.rb | 12 +++--- spec/components/search_spec.rb | 4 ++ spec/models/notification_spec.rb | 53 +++++++++++++++++++++++++ spec/models/post_alert_observer_spec.rb | 1 + spec/models/post_spec.rb | 46 --------------------- spec/models/post_timing_spec.rb | 5 ++- spec/models/topic_spec.rb | 15 ++++--- spec/models/user_action_spec.rb | 4 ++ spec/spec_helper.rb | 7 ++++ 11 files changed, 95 insertions(+), 60 deletions(-) diff --git a/spec/components/jobs/exporter_spec.rb b/spec/components/jobs/exporter_spec.rb index f354358920f..a46e8ed7d7b 100644 --- a/spec/components/jobs/exporter_spec.rb +++ b/spec/components/jobs/exporter_spec.rb @@ -153,6 +153,8 @@ describe Jobs::Exporter do end it "should send a notification to the user who started the export" do + + ActiveRecord::Base.observers.enable :all expect { Jobs::Exporter.new.execute( @exporter_args.merge( user_id: @user.id ) ) }.to change { Notification.count }.by(1) @@ -190,4 +192,4 @@ describe Jobs::Exporter do end end -end \ No newline at end of file +end diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index c152a5addc3..71e1be09c83 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -4,6 +4,10 @@ require 'topic_subtype' describe PostCreator do + before do + ActiveRecord::Base.observers.enable :all + end + let(:user) { Fabricate(:user) } it 'raises an error without a raw value' do diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 668806ac98d..2362eaa6176 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -3,6 +3,10 @@ require 'post_destroyer' describe PostDestroyer do + before do + ActiveRecord::Base.observers.enable :all + end + let(:moderator) { Fabricate(:moderator) } let(:post) { Fabricate(:post) } @@ -19,13 +23,7 @@ describe PostDestroyer do it "doesn't delete the post" do post.deleted_at.should be_blank - end - - it "updates the text of the post" do post.raw.should == I18n.t('js.post.deleted_by_author') - end - - it "creates a new version" do post.version.should == 2 end end @@ -39,7 +37,7 @@ describe PostDestroyer do post.deleted_at.should be_present end end - + context "as an admin" do before do PostDestroyer.new(admin, post).destroy diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index c071de4a53a..9aaaa7c4d62 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -5,6 +5,10 @@ require 'search' describe Search do + before do + ActiveRecord::Base.observers.enable :search_observer + end + def first_of_type(results, type) return nil if results.blank? results.each do |r| diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 2abf7c624f1..75031814055 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' describe Notification do + before do + ActiveRecord::Base.observers.enable :all + end it { should validate_presence_of :notification_type } it { should validate_presence_of :data } @@ -8,6 +11,56 @@ describe Notification do it { should belong_to :user } it { should belong_to :topic } + describe 'post' do + let(:topic) { Fabricate(:topic) } + let(:post_args) do + {user: topic.user, topic: topic} + end + + let(:coding_horror) { Fabricate(:coding_horror) } + + describe 'replies' do + + let(:post) { Fabricate(:post, post_args.merge(raw: "Hello @CodingHorror")) } + + it 'notifies the poster on reply' do + lambda { + @reply = Fabricate(:basic_reply, user: coding_horror, topic: post.topic) + }.should change(post.user.notifications, :count).by(1) + end + + it "doesn't notify the poster when they reply to their own post" do + lambda { + @reply = Fabricate(:basic_reply, user: post.user, topic: post.topic) + }.should_not change(post.user.notifications, :count).by(1) + end + end + + describe 'watching' do + it "does notify watching users of new posts" do + post = Fabricate(:post, post_args) + user2 = Fabricate(:coding_horror) + post_args[:topic].notify_watch!(user2) + lambda { + Fabricate(:post, user: post.user, topic: post.topic) + }.should change(user2.notifications, :count).by(1) + end + end + + describe 'muting' do + it "does not notify users of new posts" do + post = Fabricate(:post, post_args) + user = post_args[:user] + user2 = Fabricate(:coding_horror) + + post_args[:topic].notify_muted!(user) + lambda { + Fabricate(:post, user: user2, topic: post.topic, raw: 'hello @' + user.username) + }.should change(user.notifications, :count).by(0) + end + end + + end describe 'unread counts' do let(:user) { Fabricate(:user) } diff --git a/spec/models/post_alert_observer_spec.rb b/spec/models/post_alert_observer_spec.rb index e819278243c..a3359dc927d 100644 --- a/spec/models/post_alert_observer_spec.rb +++ b/spec/models/post_alert_observer_spec.rb @@ -4,6 +4,7 @@ require_dependency 'post_destroyer' describe PostAlertObserver do before do + ActiveRecord::Base.observers.enable :post_alert_observer ImageSorcery.any_instance.stubs(:convert).returns(false) end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 1816d3716d1..785d6a7f051 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -455,52 +455,6 @@ describe Post do end end - describe 'notifications' do - - let(:coding_horror) { Fabricate(:coding_horror) } - - describe 'replies' do - - let(:post) { Fabricate(:post, post_args.merge(raw: "Hello @CodingHorror")) } - - it 'notifies the poster on reply' do - lambda { - @reply = Fabricate(:basic_reply, user: coding_horror, topic: post.topic) - }.should change(post.user.notifications, :count).by(1) - end - - it "doesn't notify the poster when they reply to their own post" do - lambda { - @reply = Fabricate(:basic_reply, user: post.user, topic: post.topic) - }.should_not change(post.user.notifications, :count).by(1) - end - end - - describe 'watching' do - it "does notify watching users of new posts" do - post = Fabricate(:post, post_args) - user2 = Fabricate(:coding_horror) - post_args[:topic].notify_watch!(user2) - lambda { - Fabricate(:post, user: post.user, topic: post.topic) - }.should change(user2.notifications, :count).by(1) - end - end - - describe 'muting' do - it "does not notify users of new posts" do - post = Fabricate(:post, post_args) - user = post_args[:user] - user2 = Fabricate(:coding_horror) - - post_args[:topic].notify_muted!(user) - lambda { - Fabricate(:post, user: user2, topic: post.topic, raw: 'hello @' + user.username) - }.should change(user.notifications, :count).by(0) - end - end - - end describe 'after save' do diff --git a/spec/models/post_timing_spec.rb b/spec/models/post_timing_spec.rb index fb1c592d191..37642c44b7e 100644 --- a/spec/models/post_timing_spec.rb +++ b/spec/models/post_timing_spec.rb @@ -13,12 +13,15 @@ describe PostTiming do # integration test it 'processes timings correctly' do + + ActiveRecord::Base.observers.enable :all + post = Fabricate(:post) user2 = Fabricate(:coding_horror) PostAction.act(user2, post, PostActionType.types[:like]) - post.user.unread_notifications.should == 1 + post.user.unread_notifications.should == 1 post.user.unread_notifications_by_type.should == { Notification.types[:liked] => 1 } PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]]) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index de06f09f10a..31428241e59 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -182,6 +182,7 @@ describe Topic do context 'message bus' do it 'calls the message bus observer after create' do + ActiveRecord::Base.observers.enable :all MessageBusObserver.any_instance.expects(:after_create_topic).with(instance_of(Topic)) Fabricate(:topic) end @@ -365,7 +366,7 @@ describe Topic do context 'private message' do let(:coding_horror) { User.where(username: 'CodingHorror').first } let(:evil_trout) { Fabricate(:evil_trout) } - let!(:topic) { Fabricate(:private_message_topic) } + let(:topic) { Fabricate(:private_message_topic) } it "should integrate correctly" do Guardian.new(topic.user).can_see?(topic).should be_true @@ -389,12 +390,9 @@ describe Topic do let(:walter) { Fabricate(:walter_white) } context 'by username' do - it 'returns true' do - topic.invite(topic.user, walter.username).should be_true - end it 'adds walter to the allowed users' do - topic.invite(topic.user, walter.username) + topic.invite(topic.user, walter.username).should be_true topic.allowed_users.include?(walter).should be_true end @@ -426,6 +424,8 @@ describe Topic do let(:actions) { topic.user.user_actions } it "should set up actions correctly" do + ActiveRecord::Base.observers.enable :all + actions.map{|a| a.action_type}.should_not include(UserAction::NEW_TOPIC) actions.map{|a| a.action_type}.should include(UserAction::NEW_PRIVATE_MESSAGE) coding_horror.user_actions.map{|a| a.action_type}.should include(UserAction::GOT_PRIVATE_MESSAGE) @@ -435,6 +435,11 @@ describe Topic do context "other user" do + before do + # let! is weird, this test need a refactor + t = topic + end + let(:creator) { PostCreator.new(topic.user, raw: Fabricate.build(:post).raw, topic_id: topic.id )} it "sends the other user an email when there's a new post" do diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb index 8a1c3644937..5f280b2af95 100644 --- a/spec/models/user_action_spec.rb +++ b/spec/models/user_action_spec.rb @@ -2,6 +2,10 @@ require 'spec_helper' describe UserAction do + before do + ActiveRecord::Base.observers.enable :all + end + it { should validate_presence_of :action_type } it { should validate_presence_of :user_id } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bcaa409dcd8..ce1a3d32fcd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -48,6 +48,8 @@ Spork.prefork do # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} + + # let's not run seed_fu every test SeedFu.seed @@ -71,6 +73,11 @@ Spork.prefork do # config.before(:suite) do # end + config.before do + # disable all observers, enable as needed during specs + ActiveRecord::Base.observers.disable :all + end + config.before(:all) do DiscoursePluginRegistry.clear end