Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSpec - if you are gonna use me then abuse me

Krzysztof
December 21, 2023

RSpec - if you are gonna use me then abuse me

A quick presentation with some examples, about how RSpec should and can be used to write actual specs instead of just tests. It touches on differences between those two, and provides examples of useful RSpec DSL extensions.

Krzysztof

December 21, 2023
Tweet

More Decks by Krzysztof

Other Decks in Programming

Transcript

  1. RSpec - if you are gonna use me - abuse

    me 10.2022 https://2n.pl/
  2. RSpec i testy (0/11) 1. Minitest https://github.com/minitest/minitest 2. Test-bench https://github.com/test-bench/test-bench

    Both framework are principal tools, more minimalistic, and thanks to that faster and closer with their DSL to pure ruby. The difference between specs and tests is actually quite big, with many not really knowing there is one.1 Spec, is a short form of specification and according to BDD it should enforce a way of thinking and constructing the code in a way which aims to describe what does the specified software does2 You can shorten this to call specs “tests that are written for humans”.
  3. RSpec - DSL i and building real specs (4/11) My

    main point: RSpec is a tool much bigger than other testing frameworks we have in ruby. However many of its features, are not used by many of its users. In those cases, using it instead of alternatives (wspomiany MiniTest i TestBench) is a waste of resources. RSpec, despite some flaws, is the best ruby tool to build specs. It is not the best to build tests. Here i want to share some examples of how you can use its robust DSL to build better specs, that are actual specs, more than pure tests.
  4. Example 0 - matchers (5/11) Decorators, Presenters etc. are a

    common pattern in RoR. In bare RSpec there is no special DSL for this pattern since its implementations differ… no DSL means we are testing it with pure ruby which makes it more of a test not spec We can build our own DSL in RSpec and end up with this spec and here: Matcher For Decorator is: (polish word for cat is pronounced similarly to polish word for code so its funny)
  5. Example 1 - own DSL (6/11) We can add our

    own DSL to check if ServiceObject was called correctly. Check out the full example on GitHub
  6. Thoughts and prayers (10/11) RSpec gives us a lot fo

    ready to go DSL, but also says it is supposed to be a base4, under the actual specifications of out software by extending what is given to us by default. We are supposed to extend it by the specification of our app. That is written by the creators of RSpec. So using RSpec remember to write specs, not just tests. If you want just tests, look for other tools Dogcat
  7. Sources (11/11) 1. https://sites.google.com/site/unclebobconsultingllc/specs-vs-tests 2. BDD in Action: Behavior-driven development

    for the whole software lifecycle by John Ferguson Smart 3. http://test-bench.software/ 4. Effective testing with RSpec - Myron Marston & Ian Dees (edycja 3, tłumaczenie własne)