Skip to content

Instantly share code, notes, and snippets.

@jrep
Last active August 29, 2015 14:02
Show Gist options
  • Save jrep/fc3605e1d5a9f3e96cd5 to your computer and use it in GitHub Desktop.
Save jrep/fc3605e1d5a9f3e96cd5 to your computer and use it in GitHub Desktop.
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: '[email protected]'
user2 = User.create! email: '[email protected]'
assert_equal false, user2.update_attributes(id: user2.id, email: '[email protected]')
assert_raises(ActiveRecord::RecordNotUnique) { user2.update_attributes!(id: user2.id, email: '[email protected]') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment