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'
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
# 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
@ -53,15 +30,14 @@ 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.quiet = true if SeedFu.respond_to? :quiet
SeedFu.seed
RSpec.configure do |config|
config.fail_fast = ENV['RSPEC_FAIL_FAST'] == "1"
config.include Helpers
config.include MessageBus
config.mock_framework = :mocha
config.order = 'random'
@ -124,50 +100,6 @@ Spork.each_run do
$redis.client.reconnect
Rails.cache.reconnect
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
# --- Instructions ---
@ -198,5 +130,3 @@ end
#
# These instructions should self-destruct in 10 seconds. If they don't, feel
# 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