disable observers in tests, enable as needed, tests are 20% faster

This commit is contained in:
Sam 2013-05-14 11:59:55 +10:00
parent fff46cf5aa
commit ef98b60184
11 changed files with 95 additions and 60 deletions

View File

@ -153,6 +153,8 @@ describe Jobs::Exporter do
end end
it "should send a notification to the user who started the export" do it "should send a notification to the user who started the export" do
ActiveRecord::Base.observers.enable :all
expect { expect {
Jobs::Exporter.new.execute( @exporter_args.merge( user_id: @user.id ) ) Jobs::Exporter.new.execute( @exporter_args.merge( user_id: @user.id ) )
}.to change { Notification.count }.by(1) }.to change { Notification.count }.by(1)

View File

@ -4,6 +4,10 @@ require 'topic_subtype'
describe PostCreator do describe PostCreator do
before do
ActiveRecord::Base.observers.enable :all
end
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
it 'raises an error without a raw value' do it 'raises an error without a raw value' do

View File

@ -3,6 +3,10 @@ require 'post_destroyer'
describe PostDestroyer do describe PostDestroyer do
before do
ActiveRecord::Base.observers.enable :all
end
let(:moderator) { Fabricate(:moderator) } let(:moderator) { Fabricate(:moderator) }
let(:post) { Fabricate(:post) } let(:post) { Fabricate(:post) }
@ -19,13 +23,7 @@ describe PostDestroyer do
it "doesn't delete the post" do it "doesn't delete the post" do
post.deleted_at.should be_blank 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') post.raw.should == I18n.t('js.post.deleted_by_author')
end
it "creates a new version" do
post.version.should == 2 post.version.should == 2
end end
end end

View File

@ -5,6 +5,10 @@ require 'search'
describe Search do describe Search do
before do
ActiveRecord::Base.observers.enable :search_observer
end
def first_of_type(results, type) def first_of_type(results, type)
return nil if results.blank? return nil if results.blank?
results.each do |r| results.each do |r|

View File

@ -1,6 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe Notification do describe Notification do
before do
ActiveRecord::Base.observers.enable :all
end
it { should validate_presence_of :notification_type } it { should validate_presence_of :notification_type }
it { should validate_presence_of :data } it { should validate_presence_of :data }
@ -8,6 +11,56 @@ describe Notification do
it { should belong_to :user } it { should belong_to :user }
it { should belong_to :topic } 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 describe 'unread counts' do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }

View File

@ -4,6 +4,7 @@ require_dependency 'post_destroyer'
describe PostAlertObserver do describe PostAlertObserver do
before do before do
ActiveRecord::Base.observers.enable :post_alert_observer
ImageSorcery.any_instance.stubs(:convert).returns(false) ImageSorcery.any_instance.stubs(:convert).returns(false)
end end

View File

@ -455,52 +455,6 @@ describe Post do
end end
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 describe 'after save' do

View File

@ -13,12 +13,15 @@ describe PostTiming do
# integration test # integration test
it 'processes timings correctly' do it 'processes timings correctly' do
ActiveRecord::Base.observers.enable :all
post = Fabricate(:post) post = Fabricate(:post)
user2 = Fabricate(:coding_horror) user2 = Fabricate(:coding_horror)
PostAction.act(user2, post, PostActionType.types[:like]) 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 } post.user.unread_notifications_by_type.should == { Notification.types[:liked] => 1 }
PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]]) PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]])

View File

@ -182,6 +182,7 @@ describe Topic do
context 'message bus' do context 'message bus' do
it 'calls the message bus observer after create' 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)) MessageBusObserver.any_instance.expects(:after_create_topic).with(instance_of(Topic))
Fabricate(:topic) Fabricate(:topic)
end end
@ -365,7 +366,7 @@ describe Topic do
context 'private message' do context 'private message' do
let(:coding_horror) { User.where(username: 'CodingHorror').first } let(:coding_horror) { User.where(username: 'CodingHorror').first }
let(:evil_trout) { Fabricate(:evil_trout) } let(:evil_trout) { Fabricate(:evil_trout) }
let!(:topic) { Fabricate(:private_message_topic) } let(:topic) { Fabricate(:private_message_topic) }
it "should integrate correctly" do it "should integrate correctly" do
Guardian.new(topic.user).can_see?(topic).should be_true Guardian.new(topic.user).can_see?(topic).should be_true
@ -389,12 +390,9 @@ describe Topic do
let(:walter) { Fabricate(:walter_white) } let(:walter) { Fabricate(:walter_white) }
context 'by username' do 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 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 topic.allowed_users.include?(walter).should be_true
end end
@ -426,6 +424,8 @@ describe Topic do
let(:actions) { topic.user.user_actions } let(:actions) { topic.user.user_actions }
it "should set up actions correctly" do 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_not include(UserAction::NEW_TOPIC)
actions.map{|a| a.action_type}.should include(UserAction::NEW_PRIVATE_MESSAGE) 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) 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 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 )} 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 it "sends the other user an email when there's a new post" do

View File

@ -2,6 +2,10 @@ require 'spec_helper'
describe UserAction do describe UserAction do
before do
ActiveRecord::Base.observers.enable :all
end
it { should validate_presence_of :action_type } it { should validate_presence_of :action_type }
it { should validate_presence_of :user_id } it { should validate_presence_of :user_id }

View File

@ -48,6 +48,8 @@ Spork.prefork do
# in spec/support/ and its subdirectories. # in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# let's not run seed_fu every test # let's not run seed_fu every test
SeedFu.seed SeedFu.seed
@ -71,6 +73,11 @@ Spork.prefork do
# config.before(:suite) do # config.before(:suite) do
# end # end
config.before do
# disable all observers, enable as needed during specs
ActiveRecord::Base.observers.disable :all
end
config.before(:all) do config.before(:all) do
DiscoursePluginRegistry.clear DiscoursePluginRegistry.clear
end end