Easy Way to Use Shared Examples in Ruby on Rails

Ahmet Kaptan
4 min readJan 10, 2024

--

Hello folks, today I’ll be discussing shared_examples in Ruby on Rails. First of all, I want to explain DRY (Don’t Repeat Yourself) and why we should use shared_examples and how to implement it in our projects. So let’s dive in!

Writing robust tests is the foundation of developing a powerful application.

What is DRY?

DRY stands for “Don’t Repeat Yourself,” and it is a software development principle that emphasizes avoiding code duplication within a codebase. The main idea behind DRY is to promote code reusability, maintainability, and efficiency by eliminating redundancy. You might ask, ‘I follow the DRY principle while coding, so why should I apply it in spec files?’ If you think like that, you are not alone, as I had similar thoughts when I started writing tests. However, there’s a ‘BUT.’

Applying the DRY (Don’t Repeat Yourself) principle in RSpec tests offers several benefits. Firstly, it streamlines test maintenance by allowing changes in a single location, making tests more sustainable and easily managed. It also enhances the organization of the test suite, making it more understandable. Adhering to DRY not only prevents code duplication but also facilitates scaling the test suite as projects grow, ensuring an efficient addition of new scenarios. Additionally, as project requirements evolve, the DRY principle simplifies managing changes by centralizing modifications. By reducing the risk of code duplication, it enhances the reliability of the test suite. Overall, following DRY in RSpec tests promote clean, maintainable, and scalable code, thereby enhancing the efficiency of the development process.

shared_examples

In Ruby on Rails, shared_examples is a feature provided by the RSpec testing framework that allows you to define and reuse examples (sets of tests) across different parts of your test suite. It promotes the DRY (Don't Repeat Yourself) principle in your testing code.

You can define a set of common examples using the shared_examples method. This set of examples typically represents a behavior or functionality that you want to test across multiple contexts.

Let’s look 3 examples of shared_examples

  • Authorization Shared Examples:
shared_examples 'an authorized user' do
it 'has access to the resource' do
# test access to the resource for an authorized user
end

it 'can update the resource' do
# test update capability for an authorized user
end
end
describe 'Admin User' do
it_behaves_like 'an authorized user'
end

describe 'Manager User' do
it_behaves_like 'an authorized user'
end
  • Pagination Shared Examples:
shared_examples 'paginated results' do
it 'returns a limited number of records per page' do
# test pagination logic
end

it 'provides next and previous page information' do
# test next and previous page information
end
end
describe 'Products API' do
it_behaves_like 'paginated results'
end

describe 'Users API' do
it_behaves_like 'paginated results'
end
  • Cache Management Shared Examples:
shared_examples 'cacheable resource' do
it 'caches the resource for a certain period' do
# test caching behavior
end

it 'invalidates the cache on update' do
# test cache invalidation on update
end
end
describe 'Cached Articles' do
it_behaves_like 'cacheable resource'
end

describe 'Cached Users' do
it_behaves_like 'cacheable resource'
end

I have an extra example 💣

Parameterized Shared Examples:

You can also make your shared examples more flexible by passing parameters.

# Parameterized shared examples
shared_examples 'a ripe fruit' do |fruit_type|
it "is ripe #{fruit_type}" do
# test for ripeness
end
end
# Using parameterized shared examples
describe 'Banana' do
it_behaves_like 'a ripe fruit', 'banana'
end

describe 'Peach' do
it_behaves_like 'a ripe fruit', 'peach'
end

Conclusion

In this article, we explored the shared_examples feature within Ruby on Rails and examined how this feature aligns with the fundamental principle of software development, DRY (Don't Repeat Yourself). By using it, we can effectively scale our test suite, make maintenance easier, and reduce code duplication. Each shared example set represents specific behaviors or functionality, and by using these sets in different contexts, we can make our test code cleaner and more understandable.

By examining various scenarios exemplified in this article, we gained insights into how shared_examples can be utilized in real-world projects. Defining shared behaviors in a single location facilitates maintenance and contributes to making our projects more reliable.

My Last Articles:

--

--