Programowanie

28 paź 2020

Zachowaj zasadę DRY w testach - trochę o wspólnym kontekście w RSpec

Zachowaj zasadę DRY w testach - trochę o wspólnym kontekście w RSpec

Zachowaj zasadę DRY w testach - trochę o wspólnym kontekście w RSpec

Konrad Badzioch

Senior Backend Developer

Fioletowy Kwiat
Fioletowy Kwiat
Fioletowy Kwiat

As a programista, I’m always seeking to improve my tests to keep them clean and enjoyable. Shared context was the perfect rozwiązanie for reusing repeatable variables in tests.

As a programista, I constantly look for improving my code. Also in terms of tests. For me, it's important to keep tests clean in order to add new tests to be a pleasure, not a drudgery.

First time I stumbled across the shared context when I was looking for something that can help me to extract some constantly repeated variables in tests. The shared context was exactly what I was looking for. I will show you how to use it and what are the benefits of that.

What do we test?

Assume we have to write a few policy objects. They can look like this following one:

Somewhere we have defined User and UserSettings classes which instances are connected by user_id. They may resemble ActiveRecord objects and a baza danych relationship or they may be objects built based on events. Having that we can go to write tests. In this example, first_name, last_name, and email are required to create a user object.

To test the policy object we need an admin user and a user who is the owner of a resource. Obviously the resource as well.

You have to admit these lets took somewhat place. Ok, we can take care of testowanie the next policy object. Hmmmm... it means we need to declare the user and the admin in the subsequent spec file. This isn't the way.

It's time for the shared context

I've created the shared directory inside of the spec catalogue. Inside of that, I've created the users file. I named it so to accurately specify the content of the file.

The content of this file includes lets that we saw before in the policy spec file. The only difference is that I extracted ids to separate variables because I don't want to use [user.id](http://user.id) every time. There is still one thing to do, we have to load the shared context in the spec_helper.rb file, like so:

The line above loads all .rb files from the shared directory.

Having that we can start to use the shared context what is pretty simple. Let's take a look at the spec file. Now, it's clearer, is it?

As you could notice, we need to include the context by using include_context :users. After that, we can use everything that we've defined inside of our context file. Now, we can write another policy object and tests for it without duplicating the users declarations.

Summary

The shared context is a good place to put variables that we use in more than one spec file. If you need you can also put mocks in your context. Sometimes it's hard to write a shared context that not contains unused variables but no worry, unused variables are lets and they are initialized when you use them in a test file. Despite that, I really encourage you to write small shared contexts in order to easily include just what you need and keep the proper naming of context files.

✍️

O autorze

Konrad Badzioch

Senior Backend Developer

programowanie is my job and also my hobby. Besides that, I love fantasy books, cycling, and many, many things. A productively spent day makes me happy and gives me energy for the next day.

Masz pomysł na projekt? Porozmawiajmy i wspólnie go zrealizujmy.

Nasi wysoko wykwalifikowani specjaliści są do Twojej dyspozycji. Skontaktuj się z nami i przekonaj się, co możemy razem osiągnąć.

Dariusz Michalski

Dariusz Michalski, CEO

dariusz@useo.pl

Konrad Pochodaj

Konrad Pochodaj, CGO

konrad@useo.pl

Masz pomysł na projekt? Porozmawiajmy i wspólnie go zrealizujmy.

Nasi wysoko wykwalifikowani specjaliści są do Twojej dyspozycji. Skontaktuj się z nami i przekonaj się, co możemy razem osiągnąć.

Dariusz Michalski

Dariusz Michalski, CEO

dariusz@useo.pl

Konrad Pochodaj

Konrad Pochodaj, CGO

konrad@useo.pl

Masz pomysł na projekt? Porozmawiajmy i wspólnie go zrealizujmy.

Nasi wysoko wykwalifikowani specjaliści są do Twojej dyspozycji. Skontaktuj się z nami i przekonaj się, co możemy razem osiągnąć.

©2009 - 2025 Useo sp. z o.o.

Gdzie nas znaleźć

ul. Ofiar Oświęcimskich 17

50-069 Wrocław, Polska

ul. Ofiar Oświęcimskich 17

50-069 Wrocław, Polska

©2009 - 2025 Useo sp. z o.o.

Gdzie nas znaleźć

ul. Ofiar Oświęcimskich 17

50-069 Wrocław, Polska

ul. Ofiar Oświęcimskich 17

50-069 Wrocław, Polska

©2009 - 2025 Useo sp. z o.o.