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
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
end

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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]])

View File

@ -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

View File

@ -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 }

View File

@ -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