Skip to content

Instantly share code, notes, and snippets.

@jrep jrep/update_attributes.rb
Last active Aug 29, 2015

Embed
What would you like to do?
active_record_gem.rb template test case for '#update_attributes raises exception on ActiveRecord::RecordNotUnique'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.2'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users do |t|
t.string "email"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@mailinator\.com\z/i
validates :email, presence: true, uniqueness: true, format: {
with: VALID_EMAIL_REGEX,
message: "must be a Mailinator email address."
}
end
class BugTest < Minitest::Test
def test_uniqueness
user1 = User.create! email: 'user@mailinator.com'
user2 = User.create! email: 'user2@mailinator.com'
assert_equal false, user2.update_attributes(id: user2.id, email: 'User@mailinator.com')
assert_raises(ActiveRecord::RecordNotUnique) { user2.update_attributes!(id: user2.id, email: 'User@mailinator.com') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.