> For the complete documentation index, see [llms.txt](https://craftacademy.gitbook.io/coding-as-a-craft-bootcamp-prep-v2-0/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://craftacademy.gitbook.io/coding-as-a-craft-bootcamp-prep-v2-0/week-1-programming-basics-ruby/rspec/rspec-basics.md).

# RSpec Basics

The first thing we need when we start testing is a spec file for the tests we want to write. The convention dictates that we use the class name of the object we want to test, followed by the word `_spec`. Then, of course, we need to give the file a suffix - in this case `.rb` - our specs are written in ruby.

If we want to test a `Car` class, our spec file will be named `car_spec.rb`. Inside the spec file we will need to `require` the file that we are running the test for.

For example we need to `test car.rb` this means that we need to `require car.rb` in our `car_spec.rb`

car.rb should be in the lib folder and car\_spec.rb should be in the spec folder that was created when we ran the command`rspec --init` in the terminal.

## **Structure of a test file**&#x20;

Like everything we need to structure our test in order for them to make sense and for RSpec to understand what we want it to test. We will always have a describe block where we define our class or code. You can have several describe blocks inside another describe block in order for more structure in the file. You can view the describe block as a chapter for a book and the it blocks as separate pages in that book. You can also use the word the keyword `context` when you want to use a `describe` block inside another `describe` block, the keyword `context` is an alias for `describe` and gives more clarity in some situations.

![](https://939397000-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9htzNbAvfpKcVkZT-D%2F-L9hu-eH5xWIUnJXzT0n%2F-L9hu1EnwG7UJZLNqV5t%2Fcarbon%20\(16\).png?generation=1523336096524069\&alt=media)

## Expectations

Expectations are our way to tell RSpec what we want it to test. The structure of an expect statement is\
`expect().to eq()`or `expect().not_to eq()`

There are four parts to an expectation `expect`, `expect-argument`, `to` and the `to-argument` ( or `not_to` argument).\
Don't let the lack or precence of the brackets after the to fool you it is still an argument. Ruby gives us the option to skip the brackets if we want.

![](https://939397000-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9htzNbAvfpKcVkZT-D%2F-L9hu-eH5xWIUnJXzT0n%2F-L9hu1FH6OVihqE3BJll%2Fcarbon%20\(20\).png?generation=1523336108345777\&alt=media)

## The main structure

The image below shows a chart on the heirarchy on the structure of a spec suite.

![](https://939397000-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9htzNbAvfpKcVkZT-D%2F-L9hu-eH5xWIUnJXzT0n%2F-L9hu1FKg9cKP5q9fhuI%2Fcarbon%20\(21\).png?generation=1523336096467295\&alt=media)

This translates to

![](https://939397000-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9htzNbAvfpKcVkZT-D%2F-L9hu-eH5xWIUnJXzT0n%2F-L9hu1FNXcprnZU99Fcq%2Fcarbon%20\(22\).png?generation=1523336104449430\&alt=media)
