Slide 1

Slide 1 text

RSpec - if you are gonna use me - abuse me 10.2022 https://2n.pl/

Slide 2

Slide 2 text

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”.

Slide 3

Slide 3 text

Quick comparison - test bench3 (1/11)

Slide 4

Slide 4 text

Quick comparison - mini_test (test mode) (2/11)

Slide 5

Slide 5 text

Quick comparison - mini_test (spec mode) (3/11)

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Example 2 - big helpers (7/11)

Slide 10

Slide 10 text

Errors produces 2 (8/11)

Slide 11

Slide 11 text

Code behind it (9/11) There it is https://gist.git hub.com/krzykami l/7b58376270fdb3 58a45d0a7266ff23 aa

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

Dziękuję za uwagę