Example groups Rspec – Part 1

Tram Ho

Advertising precious remedy

If you are fed up with writing and watching the following lines of code repeat over and over again, this article is for you. A cure for all of you, because my family has been cured for 3 generations =)).

Somewhere there is the rspec code for the controller:

Have you tried to think that writing rspec for #index on 10 controllers like that is boring? Not to mention the #show , #edit in each controller, the quick mental math must also be repeated about ~ 30 times. (#index, #show, #edit / controller).

To cure sickness, applying DRY through shared examples is very effective.

How to use precious medicine

Please read carefully the properties and nature of the drug so that it can be used for the right cure in the right place.


In the article there are many terms used example and example group . If you do not know, then when using the describe, context in rspec, you have created an example group and it will create an example . An example group will contain many examples.

Example: A group and an example.

Example nested group:

Pharmacodynamics – Shared examples

Let’s translate the definition of shared examples : Shared examples allow you to describe the behavior of classes or modules. When declared, the content of a shared group is stored. It is only executed in the context of another example group, which provides any shared group context that needs to run.

In a simpler way, shared examples allow us to declare examples and reuse those examples in many other places. That is the most prominent feature that shared example brings and helps unit test code follow the DRY principle (minimizing repetitive code).

To use the shared examples feature we just need to do two steps:

  1. Definition of shared examples
  2. Using shared examples told in another example group.

Definition of shared examples

The way to define a shared examples is to use the shared_examples method. The definition of this method is also very clear about the function of shared_examples that is: Store block for later use. The block passed to the shared_examples method will be evaluated in another example group via include_examples , include_context or it_behaves_like .

Sample example:

Using shared examples told in another example group.

After having a shared examples use one of the following ways to use that shared example:

Sample example:

Note: shared_examples is only used in the example group if it is used in an example which will cause an error.

Distinguish include_examples and it_behaves_like

As mentioned above we see 4 ways to use a shared example but in this post we will focus on the first 3 ways and clarify the differences between them.

The definition of include_examples can be found here . Simply put, it just includes (pulls) the content of shared examples into the example group.

For example:

The essence of the above code is similar:

If you run the rspec command, you will see the following output:

Be careful when using include_examples more than once in the same example group. If in the shared example, declare multiple methods with the same name, only the last method will be taken.

Example 1:

You would expect that both of the above examples pass but the result is not.

example1 failed for the reason, so it is too clear to expect “parameter1” to get “parameter2”.

Because of the nature of the example paragraph 1 above as follows:

let (: something) has been declared twice within the scope of the example group, so only the last is accepted. Therefore the value of something returned will be “parameter2” instead of “parameter1” which will fail example1.

So when using include_examples you need to pay attention to what the content of the shared example is and the scope of the example group that uses it.

To overcome the above problem you can use it_behaves_like or it_should_behave_like . Both are the same, only different names. They do one more thing than include_examples is to create an example group that wraps the examples inside the shared example.

For example:

As expected, both examples pass.

From the above image you can easily see outside the example “uses the given parameter” inside the example group is “behaves like some example”. That’s the only difference between include_examples and it_behaves_like . For safety, easy to confuse and difficult to control, you can use it_behaves_like instead of include_examples .


In this article, we only focus on the shared example and the difference between include_example and it_behaves_like . Besides, there are many other uses of shared examples here . After this article, we hope readers will apply many things when writing rspec for their projects. As well as a better understanding of rspec in general and examples in particular. If you see the same example and repeat many times, think about the shared example.

How to solve the problem given at the beginning of the article you can use a shared example with parameters offline.

Thanks for reading to my post!


  1. https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
Share the news now

Source : Viblo