Introducing Filtering and Formatters in Rspec

Tram Ho

I. Introduction

Rspec is a framework use to testing for Ruby. It’s a little bit different to another frameworks by easy to read syntax and understanding by people who do not have much programming knowledge. Rspec mostly focus on how you describe the behavior of your application and it does’t mention about your process or flow in your application. In this article, I will show you about 2 feature in rspec: filtering and formatter.

II. Filtering

Every example in Rspec has associated metadata and you can append arbitrary information to it. By add metadata with any value you want, you can run rspec and filtering them by value you set to metadata.
Example: now we have a file rspec: spec/sample_spec.rb as below:

After the test run sample_spec.rb we get the results:

Now we add metadata first and second into our file sample_spec.rb as below:

By adding the metadata , now you can filter example to run by command below:

  • In case: first: true

you can see the result only example was run:

  • In case: second: true and result as below:

III. Formatters

Formatters allow RSpec to display test results output in a variety of ways. Rspec provide you some formatters and use it with output result that you want . Another way, RSpec allows customization of the output by creating your own Formatter class.
Below are some Formatters that you can use in Rspec:

  • progress (default) – Prints dots for passing examples, F for failures, * for pending.
  • documentation – Prints the docstrings passed to describe and it methods (and their aliases).
  • html
  • json – Useful for archiving data for subsequent analysis.
    The progress formatter is the default, anyway you can choose one or more of the other formatters by passing with the --format (or -f for short) command-line option.

Suppose we have a file spec/sample_spec.rb:

Let’s try running with option --format progress:

After we run the command you can see no different to what we run without --format progress. Rspec will set --format progress as defualt format.

Let’s try running with option --format documentation:


IV. Conclusion

I hope this article can help all of you to understand more about Rspec freature. Especially, about the 2 feature that i talk above:

  • How to filtering example that you want to run by add metadata.
  • How to format the output result with Rspec formatters.
Share the news now

Source : Viblo