Skip to content

Commit 232c855

Browse files
Fix tests with Rails main
Rails main / 7.1.0.alpha introduced a change to improve typography by default, by converting all apostrophes to be single quotation marks. rails/rails#45463 The change caused all our text based matching to fail, this updates the tests to ensure compatibility. Model tests were changed to test against the error type & information rather than the translated string, which I think is an improvement overall that should make them a little less brittle. I thought of using [of_kind?] but that isn't available on all Rails versions we currently support, while `added?` is. The drawback is that `added?` require full details like the `:confirmation` example which requires the related attribute that is being confirmed, but that's a small price to pay. Integration tests were changed to match on a regexp that accepts both quotes. I could've used a simple `.` to match anything there, but thought I'd just keep it specific for clarity on what it is really expected to match there. Plus, since it's integration testing against a rendered response body, it's better to match the actual text rather than resort on other ways. (like using I18n directly, etc.) [of_kind?] https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-of_kind-3F
1 parent afec665 commit 232c855

9 files changed

+32
-32
lines changed

test/integration/confirmable_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ def resend_confirmation
178178
test "should not be able to confirm an email with a blank confirmation token" do
179179
visit_user_confirmation_with_token("")
180180

181-
assert_contain "Confirmation token can't be blank"
181+
assert_contain %r{Confirmation token can['’]t be blank}
182182
end
183183

184184
test "should not be able to confirm an email with a nil confirmation token" do
185185
visit_user_confirmation_with_token(nil)
186186

187-
assert_contain "Confirmation token can't be blank"
187+
assert_contain %r{Confirmation token can['’]t be blank}
188188
end
189189

190190
test "should not be able to confirm user with blank confirmation token" do
@@ -193,7 +193,7 @@ def resend_confirmation
193193

194194
visit_user_confirmation_with_token("")
195195

196-
assert_contain "Confirmation token can't be blank"
196+
assert_contain %r{Confirmation token can['’]t be blank}
197197
end
198198

199199
test "should not be able to confirm user with nil confirmation token" do
@@ -202,7 +202,7 @@ def resend_confirmation
202202

203203
visit_user_confirmation_with_token(nil)
204204

205-
assert_contain "Confirmation token can't be blank"
205+
assert_contain %r{Confirmation token can['’]t be blank}
206206
end
207207

208208
test 'error message is configurable by resource name' do

test/integration/recoverable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def reset_password(options = {}, &block)
173173
assert_response :success
174174
assert_current_url '/users/password'
175175
assert_have_selector '#error_explanation'
176-
assert_contain "Password confirmation doesn't match Password"
176+
assert_contain %r{Password confirmation doesn['’]t match Password}
177177
assert_not user.reload.valid_password?('987654321')
178178
end
179179

test/integration/registerable_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def user_sign_up
112112
assert_template 'registrations/new'
113113
assert_have_selector '#error_explanation'
114114
assert_contain "Email is invalid"
115-
assert_contain "Password confirmation doesn't match Password"
115+
assert_contain %r{Password confirmation doesn['’]t match Password}
116116
assert_contain "2 errors prohibited"
117117
assert_nil User.to_adapter.find_first
118118

@@ -251,7 +251,7 @@ def user_sign_up
251251
fill_in 'current password', with: '12345678'
252252
click_button 'Update'
253253

254-
assert_contain "Password confirmation doesn't match Password"
254+
assert_contain %r{Password confirmation doesn['’]t match Password}
255255
assert_not User.to_adapter.find_first.valid_password?('pas123')
256256
end
257257

test/models/authenticatable_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class AuthenticatableTest < ActiveSupport::TestCase
3030

3131
test 'find_or_initialize_with_errors adds blank error' do
3232
user_with_error = User.find_or_initialize_with_errors([:email], { email: "" })
33-
assert_equal ["Email can't be blank"], user_with_error.errors.full_messages_for(:email)
33+
assert user_with_error.errors.added?(:email, :blank)
3434
end
3535

3636
test 'find_or_initialize_with_errors adds invalid error' do
3737
user_with_error = User.find_or_initialize_with_errors([:email], { email: "example@example.com" })
38-
assert_equal ["Email is invalid"], user_with_error.errors.full_messages_for(:email)
38+
assert user_with_error.errors.added?(:email, :invalid)
3939
end
4040

4141
if defined?(ActionController::Parameters)

test/models/confirmable_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def setup
7474
test 'should return a new record with errors when a blank token is given' do
7575
confirmed_user = User.confirm_by_token('')
7676
assert_not confirmed_user.persisted?
77-
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
77+
assert confirmed_user.errors.added?(:confirmation_token, :blank)
7878
end
7979

8080
test 'should return a new record with errors when a blank token is given and a record exists on the database' do
@@ -83,7 +83,7 @@ def setup
8383
confirmed_user = User.confirm_by_token('')
8484

8585
assert_not user.reload.confirmed?
86-
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
86+
assert confirmed_user.errors.added?(:confirmation_token, :blank)
8787
end
8888

8989
test 'should return a new record with errors when a nil token is given and a record exists on the database' do
@@ -92,7 +92,7 @@ def setup
9292
confirmed_user = User.confirm_by_token(nil)
9393

9494
assert_not user.reload.confirmed?
95-
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
95+
assert confirmed_user.errors.added?(:confirmation_token, :blank)
9696
end
9797

9898
test 'should generate errors for a user email if user is already confirmed' do
@@ -314,7 +314,7 @@ def setup
314314
user = create_user
315315
confirm_user = User.send_confirmation_instructions(email: user.email)
316316
assert_not confirm_user.persisted?
317-
assert_equal "can't be blank", confirm_user.errors[:username].join
317+
assert confirm_user.errors.added?(:username, :blank)
318318
end
319319
end
320320

test/models/database_authenticatable_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def setup
172172
assert_not user.update_with_password(password: 'pass4321',
173173
password_confirmation: 'pass4321')
174174
assert user.reload.valid_password?('12345678')
175-
assert_match "can't be blank", user.errors[:current_password].join
175+
assert user.errors.added?(:current_password, :blank)
176176
end
177177

178178
test 'should run validations even when current password is invalid or blank' do
@@ -181,7 +181,7 @@ def setup
181181
assert user.persisted?
182182
assert_not user.update_with_password(username: "")
183183
assert_match "usertest", user.reload.username
184-
assert_match "can't be blank", user.errors[:username].join
184+
assert user.errors.added?(:username, :blank)
185185
end
186186

187187
test 'should ignore password and its confirmation if they are blank' do
@@ -235,7 +235,7 @@ def setup
235235
user = create_user
236236
assert_not user.destroy_with_password(nil)
237237
assert user.persisted?
238-
assert_match "can't be blank", user.errors[:current_password].join
238+
assert user.errors.added?(:current_password, :blank)
239239
end
240240

241241
test 'should not email on password change' do

test/models/lockable_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def setup
213213
test 'should return a new record with errors when a blank token is given' do
214214
locked_user = User.unlock_access_by_token('')
215215
assert_not locked_user.persisted?
216-
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
216+
assert locked_user.errors.added?(:unlock_token, :blank)
217217
end
218218

219219
test 'should find a user to send unlock instructions' do
@@ -246,7 +246,7 @@ def setup
246246
user = create_user
247247
unlock_user = User.send_unlock_instructions(email: user.email)
248248
assert_not unlock_user.persisted?
249-
assert_equal "can't be blank", unlock_user.errors[:username].join
249+
assert unlock_user.errors.added?(:username, :blank)
250250
end
251251
end
252252

test/models/recoverable_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ def setup
134134
end
135135

136136
test 'should require all reset_password_keys' do
137-
swap Devise, reset_password_keys: [:username, :email] do
138-
user = create_user
139-
reset_password_user = User.send_reset_password_instructions(email: user.email)
140-
assert_not reset_password_user.persisted?
141-
assert_equal "can't be blank", reset_password_user.errors[:username].join
142-
end
137+
swap Devise, reset_password_keys: [:username, :email] do
138+
user = create_user
139+
reset_password_user = User.send_reset_password_instructions(email: user.email)
140+
assert_not reset_password_user.persisted?
141+
assert reset_password_user.errors.added?(:username, :blank)
142+
end
143143
end
144144

145145
test 'should reset reset_password_token before send the reset instructions email' do
@@ -173,7 +173,7 @@ def setup
173173
test 'should return a new record with errors if reset_password_token is blank' do
174174
reset_password_user = User.reset_password_by_token(reset_password_token: '')
175175
assert_not reset_password_user.persisted?
176-
assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
176+
assert reset_password_user.errors.added?(:reset_password_token, :blank)
177177
end
178178

179179
test 'should return a new record with errors if password is blank' do
@@ -182,7 +182,7 @@ def setup
182182

183183
reset_password_user = User.reset_password_by_token(reset_password_token: raw, password: '')
184184
assert_not reset_password_user.errors.empty?
185-
assert_match "can't be blank", reset_password_user.errors[:password].join
185+
assert reset_password_user.errors.added?(:password, :blank)
186186
assert_equal raw, reset_password_user.reset_password_token
187187
end
188188

@@ -192,7 +192,7 @@ def setup
192192

193193
reset_password_user = User.reset_password_by_token(reset_password_token: raw)
194194
assert_not reset_password_user.errors.empty?
195-
assert_match "can't be blank", reset_password_user.errors[:password].join
195+
assert reset_password_user.errors.added?(:password, :blank)
196196
assert_equal raw, reset_password_user.reset_password_token
197197
end
198198

test/models/validatable_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ValidatableTest < ActiveSupport::TestCase
88
user = new_user(email: nil)
99
assert user.invalid?
1010
assert user.errors[:email]
11-
assert_equal 'can\'t be blank', user.errors[:email].join
11+
assert user.errors.added?(:email, :blank)
1212
end
1313

1414
test 'should require uniqueness of email if email has changed, allowing blank' do
@@ -52,14 +52,14 @@ class ValidatableTest < ActiveSupport::TestCase
5252
test 'should require password to be set when creating a new record' do
5353
user = new_user(password: '', password_confirmation: '')
5454
assert user.invalid?
55-
assert_equal 'can\'t be blank', user.errors[:password].join
55+
assert user.errors.added?(:password, :blank)
5656
end
5757

5858
test 'should require confirmation to be set when creating a new record' do
5959
user = new_user(password: 'new_password', password_confirmation: 'blabla')
6060
assert user.invalid?
6161

62-
assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
62+
assert user.errors.added?(:password_confirmation, :confirmation, attribute: "Password")
6363
end
6464

6565
test 'should require password when updating/resetting password' do
@@ -69,15 +69,15 @@ class ValidatableTest < ActiveSupport::TestCase
6969
user.password_confirmation = ''
7070

7171
assert user.invalid?
72-
assert_equal 'can\'t be blank', user.errors[:password].join
72+
assert user.errors.added?(:password, :blank)
7373
end
7474

7575
test 'should require confirmation when updating/resetting password' do
7676
user = create_user
7777
user.password_confirmation = 'another_password'
7878
assert user.invalid?
7979

80-
assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
80+
assert user.errors.added?(:password_confirmation, :confirmation, attribute: "Password")
8181
end
8282

8383
test 'should require a password with minimum of 7 characters' do

0 commit comments

Comments
 (0)