Skip to content

Instantly share code, notes, and snippets.

@supairish
Created July 2, 2021 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supairish/5d763349f884b0e2417a2e9ce9a79685 to your computer and use it in GitHub Desktop.
Save supairish/5d763349f884b0e2417a2e9ce9a79685 to your computer and use it in GitHub Desktop.
Rails mini file test example
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "rails", path: "."
gem "sqlite3"
if RUBY_VERSION >= "3.1"
# net-smtp, net-imap and net-pop were removed from default gems in Ruby 3.1, but is used by the `mail` gem.
# So we need to add them as dependencies until `mail` is fixed: https://github.com/mikel/mail/pull/1439
gem "net-smtp", require: false
end
end
require "active_record/railtie"
require "active_storage/engine"
require "action_mailbox/engine"
require "tmpdir"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: {
root: Dir.tmpdir,
service: "Disk"
},
email: {
root: "#{Dir.tmpdir}/email",
service: "Disk"
}
}
# Uncomment the line below to follow active_storage
# config.action_mailbox.storage_service = config.active_storage.service
config.action_mailbox.ingress = :relay
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
require ActionMailbox::Engine.root.join("db/migrate/20180917164000_create_action_mailbox_tables.rb").to_s
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
CreateActionMailboxTables.new.change
end
class ApplicationMailbox < ActionMailbox::Base
end
require "minitest/autorun"
class RepliesMailboxTest < ActionMailbox::TestCase
test "should follow current default storage service" do
# ActiveStorage is configured to local
assert_equal(:local, ActiveStorage::Blob.service.name)
# Let's change it
ActiveStorage::Blob.service = ActiveStorage::Blob.services.fetch(:email)
assert_equal(:email, ActiveStorage::Blob.service.name)
message = create_inbound_email_from_mail(from: "david@loudthinking.com", subject: "Hello!")
raw = message.raw_email
assert(ActiveStorage::Blob.services.fetch(:email).exist?(raw.key))
assert_not(ActiveStorage::Blob.services.fetch(:local).exist?(raw.key))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment