Module: Fustrate::Rails::SpecHelper

Includes:
ActiveSupport::Testing::TimeHelpers
Defined in:
lib/fustrate/rails/spec_helper.rb

Instance Method Summary collapse

Instance Method Details

#accept(mime) ⇒ Object



11
12
13
# File 'lib/fustrate/rails/spec_helper.rb', line 11

def accept(mime)
  request.accept = mime['/'] ? mime : ::Mime::Type.lookup_by_extension(mime)
end

#data_from_file(name, **interpolations) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fustrate/rails/spec_helper.rb', line 24

def data_from_file(name, **interpolations)
  ::Dir.glob(::Rails.root.join('spec', 'data', "#{name}.*")) do |filename|
    case ::File.extname filename
    when '.yml', '.yaml'
      return yaml_data_from_file(filename, **interpolations)
    else
      raise ::ArgumentError, "Could not parse unknown file type #{::File.extname filename}"
    end
  end
end

#double_list(name, count, **stubs) ⇒ Object



22
# File 'lib/fustrate/rails/spec_helper.rb', line 22

def double_list(name, count, **stubs) = ::Array.new(count) { instance_double(name, stubs) }

#error_t(model, attribute, key) ⇒ Object

Just helps to shorten some absurdly long error message keys



16
17
18
19
20
# File 'lib/fustrate/rails/spec_helper.rb', line 16

def error_t(model, attribute, key)
  key = "attributes.#{attribute}.#{key}" if attribute

  ::I18n.t "activerecord.errors.models.#{model.model_name.i18n_key}.#{key}"
end

#image_file(filename, type = 'image/jpeg') ⇒ Object



35
36
37
# File 'lib/fustrate/rails/spec_helper.rb', line 35

def image_file(filename, type = 'image/jpeg')
  ::Rack::Test::UploadedFile.new ::Rails.root.join('spec', 'files', filename), type
end

#pdf_file(filename) ⇒ Object



39
40
41
# File 'lib/fustrate/rails/spec_helper.rb', line 39

def pdf_file(filename)
  ::Rack::Test::UploadedFile.new ::Rails.root.join('spec', 'files', filename), 'application/pdf'
end

#stfuObject

A few cron scripts use ‘puts` and clog up STDOUT.



44
45
46
47
48
49
50
51
52
53
# File 'lib/fustrate/rails/spec_helper.rb', line 44

def stfu
  orig_stdout = $stdout.clone
  $stdout.reopen ::File.new('/dev/null', 'w')
  yield
rescue ::StandardError => e
  $stdout.reopen orig_stdout
  raise e
ensure
  $stdout.reopen orig_stdout
end