---
title: 'This Week in #dev (Apr 7, 2023)'
teaser: 'Eliminating dead code, obsolete methods, and mysterious test failures on
  newest Rails.

  '
tags: this week in dev,til,rails,javascript,ruby,testing
author: thoughtbot
published_on: 2023-04-14
---

Welcome to another edition of [This Week in #dev], a series of posts where we
bring some of the most interesting Slack conversations to the public. Today
we're talking about eliminating dead code, obsolete methods and mysterious test
failures on newest Rails.

[This Week in #dev]: https://thoughtbot.com/blog/tags/this-week-in-dev

## Tree Shaking

[Sarah Lima] learned about the term "Tree Shaking," which refers to the removal
of dead code in JavaScript. The term and concept were popularized by the ES2015
module bundler [Rollup].

[Rollup]: https://github.com/rollup/rollup

## `URI.escape`: Obsolete and Removed

[Summer ☀️] shares that [`URI.escape`] was [deprecated in Ruby 1.9.2] but
popular gems were still using it. However, [ten years later] in Ruby 3.0, it was
finally removed. This can often cause errors in Ruby 2→3 upgrades.

[`URI.escape`]: https://docs.ruby-lang.org/en/2.7.0/URI/Escape.html#method-i-escape
[deprecated in Ruby 1.9.2]: https://github.com/ruby/ruby/commit/238b979f1789f95262a267d8df6239806f2859cc
[ten years later]: https://github.com/ruby/ruby/commit/abbd3241522495e8d8634c0c01a42453f76ce6b8

## New Rails, New Errors

If you upgrade to latest Rails, [Sean Doyle] says, you might see errors similar
to these in your tests:

```sh
expected to find text "can't be blank" in "Validate\nSubject can’t be blank Content Create Message"
```

That happens because [recent PR] in Rails replaced `'` with `’` in some default
Active Model [translation configurations].

### What We Recommend

[Mike Burns]' advice here is to prevent this errors altogether by using using i18n helpers
in tests to avoid asserting on bare strings:

```rb
expect(page).to have_text(I18n.t("errors.messages.blank"))
```

If you're dealing with model specs, [Matheus Richard] and [Summer ☀️] recommend
leveraging [`ActiveModel::Errors`] to assert on validation errors:

```rb
# Expect a specific type of validation error for an attribute, such as :blank
expect(model.errors).to be_added(:name, :error_type)
expect(model.errors).to be_of_kind(:name, :error_type)

# Expect any validation error for an attribute
expect(model.errors).to include(:name)
expect(model.errors).to have_key(:name)
```

[recent PR]: https://github.com/rails/rails/pull/45463
[translation configurations]: https://github.com/rails/rails/blame/main/activemodel/lib/active_model/locale/en.yml#L15-L16
[`ActiveModel::Errors`]: https://api.rubyonrails.org/classes/ActiveModel/Errors.html

## Thanks

This edition was brought to you by: [Matheus Richard], [Mike Burns], [Sarah
Lima], [Summer ☀️] and [Sean Doyle]. Thanks to all contributors! 🎉

[Matheus Richard]: https://thoughtbot.com/blog/authors/matheus-richard
[Mike Burns]: https://thoughtbot.com/blog/authors/mike-burns
[Sarah Lima]: https://thoughtbot.com/blog/authors/sarah-lima
[Summer ☀️]: https://thoughtbot.com/blog/authors/summer
[Sean Doyle]: https://thoughtbot.com/blog/authors/sean-doyle
