Skip to content

Commit

Permalink
add redundant require article
Browse files Browse the repository at this point in the history
  • Loading branch information
glaucocustodio committed Aug 7, 2024
1 parent 2bec6d2 commit 435fe4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
5 changes: 4 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
title: Even Better Specs
description: Guidelines for readable and maintainable tests
description: Guidelines for maintainable tests
theme: just-the-docs

url: https://evenbetterspecs.github.io
Expand Down Expand Up @@ -42,6 +42,9 @@ nav_external_links:
- title: Expect vs should
url: "#expect-vs-should"
hide_icon: true
- title: Redundant require
url: "#redundant-require"
hide_icon: true
- title: Instance double over double
url: "#instance-double-over-double"
hide_icon: true
Expand Down
2 changes: 1 addition & 1 deletion _includes/factories_not_fixtures.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2 id="factories-not-fixtures">
</a>
</h2>

<p><a href="https://github.com/thoughtbot/factory_bot" target="_blank">Factories</a> are more flexible and easier to work with.</p>
<p><a href="https://github.com/thoughtbot/factory_bot" target="_blank">Factories</a> are more flexible and easier to work with. Understand more <a href="https://thoughtbot.com/blog/we-need-to-talk-about-fixtures" target="_blank">here</a>.</p>

<div class="example">
{% highlight ruby %}
Expand Down
8 changes: 4 additions & 4 deletions _includes/lets_not.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ <h2 id="lets-not">
</a>
</h2>

<p>Do not use <code>let</code> / <code>let!</code>. These tend to turn your tests very complicated over time as one needs to look up variables defined then apply deltas to figure their current state. Understand more <a href="https://thoughtbot.com/blog/lets-not">here</a>.</p>
<p>Do not use <code>let</code> / <code>let!</code>. These tend to turn your tests very complicated over time as one needs to look up variables defined then apply deltas to figure their current state. Understand more <a href="https://thoughtbot.com/blog/lets-not" target="_blank">here</a>.</p>

<p class="note info">Tests are not supposed to be DRY, but easy to read and maintain.</p>

<div class="bad">
{% highlight ruby %}

describe '#full_name' do
let(:user) { create(:user, fist_name: 'Edson', last_name: 'Pelé') }
let(:user) { build(:user, fist_name: 'Edson', last_name: 'Pelé') }

context 'when first name and last name are present' do
it 'returns the full name' do
Expand All @@ -36,15 +36,15 @@ <h2 id="lets-not">
describe '#full_name' do
context 'when first name and last name are present' do
it 'returns the full name' do
user = create(:user, fist_name: 'Edson', last_name: 'Pelé')
user = build(:user, fist_name: 'Edson', last_name: 'Pelé')

expect(user.full_name).to eq('Edson Pelé')
end
end

context 'when last name is not present' do
it 'returns the first name' do
user = create(:user, fist_name: 'Edson', last_name: nil)
user = build(:user, fist_name: 'Edson', last_name: nil)

expect(user.full_name).to eq('Edson')
end
Expand Down
35 changes: 35 additions & 0 deletions _includes/redundant_require.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<article>
<h2 id="redundant-require">
<a href="#redundant-require">
Redundant require
</a>
</h2>

<p>Remove any redundant <code>require</code> in your spec files. Use <code>.rspec</code> file instead.</p>

<div class="bad">
{% highlight ruby %}
# spec/models/user_spec.rb
require 'rails_helper'

describe User do
end
{% endhighlight %}
</div>

<div class="good">
{% highlight ruby %}
# spec/models/user_spec.rb
describe User do
end
{% endhighlight %}
</div>

<div class="good">
{% highlight ruby %}
# .rspec
--require rails_helper
{% endhighlight %}
</div>

</article>
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>

<p>Event Better Specs is an <i>opinionated</i> set of best practices to support the creation of tests that are easy to read and maintain.</p>

<p>It focus on the <a href="https://www.ruby-lang.org/en/" target="_blank">Ruby</a> testing framework <a href="https://rspec.info/" target="_blank">RSpec</a>, but some of it could potentially be applied to other frameworks (like <a href="https://github.com/sus-rb/sus" target="_blank">sus</a>) and languages (like <a href="https://crystal-lang.org/reference/1.13/guides/testing.html" target="_blank">Crystal</a>).</p>
<p>It focus on the <a href="https://www.ruby-lang.org/en/" target="_blank">Ruby</a> testing framework <a href="https://rspec.info/" target="_blank">RSpec</a>, but some of it could potentially be applied to other frameworks and languages (like <a href="https://github.com/sus-rb/sus" target="_blank">sus</a> and <a href="https://crystal-lang.org/reference/1.13/guides/testing.html" target="_blank">Crystal</a>).</p>

<h2>
Guiding principles
Expand All @@ -35,6 +35,7 @@ <h2>
{% include all_possible_cases.html %}
{% include request_vs_controller.html %}
{% include expect_vs_should.html %}
{% include redundant_require.html %}
{% include instance_double_over_double.html %}
{% include lets_not.html %}
{% include avoid_hooks.html %}
Expand Down

0 comments on commit 435fe4b

Please sign in to comment.