1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/models/attached_test.rb
bogdanvlviv 8bc9062aee
Refactor activestorage/test/models/attached_test.rb
Don't include `ActiveJob::TestHelper` since there is no test that uses it.

Ensure removing of overridden User's methods.

Related to https://github.com/rails/rails/pull/33085#issuecomment-395548563
Module#remove_method is private in Ruby 2.4.

Related to fd0bd1bf68
2018-06-07 23:29:03 +03:00

56 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
setup do
@user = User.create!(name: "Josh")
end
teardown { ActiveStorage::Blob.all.each(&:purge) }
test "overriding has_one_attached methods works" do
# attach blob before messing with getter, which breaks `#attach`
@user.avatar.attach create_blob(filename: "funky.jpg")
# inherited only
assert_equal "funky.jpg", @user.avatar.filename.to_s
begin
User.class_eval do
def avatar
super.filename.to_s.reverse
end
end
# override with super
assert_equal "funky.jpg".reverse, @user.avatar
ensure
User.send(:remove_method, :avatar)
end
end
test "overriding has_many_attached methods works" do
# attach blobs before messing with getter, which breaks `#attach`
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
# inherited only
assert_equal "funky.jpg", @user.highlights.first.filename.to_s
assert_equal "wonky.jpg", @user.highlights.second.filename.to_s
begin
User.class_eval do
def highlights
super.reverse
end
end
# override with super
assert_equal "wonky.jpg", @user.highlights.first.filename.to_s
assert_equal "funky.jpg", @user.highlights.second.filename.to_s
ensure
User.send(:remove_method, :highlights)
end
end
end