RSpec introduction
What is Rspec?
A testing framework used for automated tests. It stands for Ruby Specifications. We will use Rspec for unit test in this bootcamp, even though there is a possibility to use Rspec for acceptance tests as well. RSpec is written in Ruby but it also uses a DSL (domain-specific language) This means that even though we are using Ruby when writing specs there are also built in methods in RSpec that does things their own way.
So what is a spec anyway?
The definition is as following:
A Spec (also called test/example) is a executable example that tests whether a portion of code exhibits the expected behavior in a controlled context. In simpler terms this means:
Given some context,
When some event occurs,
Then I should expect some outcome.
Given-When-Then
Do take note that this structure isverysimilar to the structure of user stories.
Coincidence? I think not!
Why should we test?
There are multiple reasons for this but the most important reasons are:
Find bugs in the code
Increases your critical thinking (forces you to think about the code you are writing)
Cover edge cases (like unexpected use of other languages, numbers etc)
Increases your confidence (deploying code, adding features, adding gems)
There are also some occasions when testing is not recommended or unnecessary.
You should not test basic ruby. This means that you are testing built in ruby methods. There are some other instances where testing is unnecessary, and I encourage you to think about what situations there are where testing is unnecessary.
Last updated