Merge pull request #1413 from eparreno/chore/refactor_spec_helper

refactor spec_helper
This commit is contained in:
Sam 2013-09-05 05:22:25 -07:00
commit dba1d79de2
3 changed files with 69 additions and 71 deletions

View file

@ -11,29 +11,6 @@ require 'spork'
require 'fakeweb' require 'fakeweb'
FakeWeb.allow_net_connect = false FakeWeb.allow_net_connect = false
module Helpers
def self.next_seq
@next_seq = (@next_seq || 0) + 1
end
def log_in(fabricator=nil)
user = Fabricate(fabricator || :user)
log_in_user(user)
user
end
def log_in_user(user)
session[:current_user_id] = user.id
end
def fixture_file(filename)
return '' if filename == ''
file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
File.read(file_path)
end
end
Spork.prefork do Spork.prefork do
# Loading more in this block will cause your tests to run faster. However, # Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll # if you change any configuration or code from libraries loaded here, you'll
@ -53,15 +30,14 @@ 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.quiet = true if SeedFu.respond_to? :quiet SeedFu.quiet = true if SeedFu.respond_to? :quiet
SeedFu.seed SeedFu.seed
RSpec.configure do |config| RSpec.configure do |config|
config.fail_fast = ENV['RSPEC_FAIL_FAST'] == "1" config.fail_fast = ENV['RSPEC_FAIL_FAST'] == "1"
config.include Helpers config.include Helpers
config.include MessageBus
config.mock_framework = :mocha config.mock_framework = :mocha
config.order = 'random' config.order = 'random'
@ -124,50 +100,6 @@ Spork.each_run do
$redis.client.reconnect $redis.client.reconnect
Rails.cache.reconnect Rails.cache.reconnect
MessageBus.after_fork MessageBus.after_fork
end
def build(*args)
Fabricate.build(*args)
end
def create_topic(args={})
args[:title] ||= "This is my title #{Helpers.next_seq}"
user = args.delete(:user) || Fabricate(:user)
guardian = Guardian.new(user)
TopicCreator.create(user, guardian, args)
end
def create_post(args={})
args[:title] ||= "This is my title #{Helpers.next_seq}"
args[:raw] ||= "This is the raw body of my post, it is cool #{Helpers.next_seq}"
args[:topic_id] = args[:topic].id if args[:topic]
user = args.delete(:user) || Fabricate(:user)
PostCreator.create(user, args)
end
module MessageBus::DiagnosticsHelper
def publish(channel, data, opts = nil)
id = super(channel, data, opts)
if @tracking
m = MessageBus::Message.new(-1, id, channel, data)
m.user_ids = opts[:user_ids] if opts
m.group_ids = opts[:group_ids] if opts
@tracking << m
end
id
end
def track_publish
@tracking = tracking = []
yield
@tracking = nil
tracking
end
end
module MessageBus
extend MessageBus::DiagnosticsHelper
end end
# --- Instructions --- # --- Instructions ---
@ -198,5 +130,3 @@ end
# #
# These instructions should self-destruct in 10 seconds. If they don't, feel # These instructions should self-destruct in 10 seconds. If they don't, feel
# free to delete them. # free to delete them.

View file

@ -0,0 +1,23 @@
module MessageBus::DiagnosticsHelper
def publish(channel, data, opts = nil)
id = super(channel, data, opts)
if @tracking
m = MessageBus::Message.new(-1, id, channel, data)
m.user_ids = opts[:user_ids] if opts
m.group_ids = opts[:group_ids] if opts
@tracking << m
end
id
end
def track_publish
@tracking = tracking = []
yield
@tracking = nil
tracking
end
end
module MessageBus
extend MessageBus::DiagnosticsHelper
end

45
spec/support/helpers.rb Normal file
View file

@ -0,0 +1,45 @@
module Helpers
def self.next_seq
@next_seq = (@next_seq || 0) + 1
end
def log_in(fabricator=nil)
user = Fabricate(fabricator || :user)
log_in_user(user)
user
end
def log_in_user(user)
session[:current_user_id] = user.id
end
def fixture_file(filename)
return '' if filename.blank?
file_path = File.expand_path(Rails.root + 'spec/fixtures/' + filename)
File.read(file_path)
end
def build(*args)
Fabricate.build(*args)
end
def create_topic(args={})
args[:title] ||= "This is my title #{Helpers.next_seq}"
user = args.delete(:user) || Fabricate(:user)
guardian = Guardian.new(user)
TopicCreator.create(user, guardian, args)
end
def create_post(args={})
args[:title] ||= "This is my title #{Helpers.next_seq}"
args[:raw] ||= "This is the raw body of my post, it is cool #{Helpers.next_seq}"
args[:topic_id] = args[:topic].id if args[:topic]
user = args.delete(:user) || Fabricate(:user)
PostCreator.create(user, args)
end
def generate_username(length=10)
range = [*'a'..'z']
Array.new(length){range.sample}.join
end
end