Rails 6 API Tutorial - RSpec API Tests POST/DELETE p.10

preview_player
Показать описание
In this video series we will build a Rails API from scratch. Backend APIs are useful for serving data to frontend applications, mobile apps or other backend services.

This video covers:
00:30 - Wrap tests for each controller/action in their own 'describe' block
01:08 - Add a 'describe' block to test the create controller/action
02:25 - Add an 'it' block to test the creating a new book
02:30 - Make POST request with API params
03:56 - Add an assertion to check that response code is 201
04:31 - Add an assertion to check that a record was created in the database, using RSpec 'to change'
05:58 - Add a 'describe' and 'it' block to test the destroy controller/action
06:50 - Make DELETE request with record ID
07:10 - Add an assertion to check that response code is 204
07:26 - Add FactoryBot call so there is a database record to delete
08:31 - Remove hardcoded ID by assigning the FactoryBot record to a variable
09:20 - Improve the specs using RSpec let
10:31 - Add an assertion to check that a record was deleted from the database, using RSpec 'to change'
11:32 - Using RSpec let! to ensure database record is created before the test gets executed
12:18 - Refactoring test setup using 'before do'

Рекомендации по теме
Комментарии
Автор

Great job, you have done exactly what the the API needs to tested, simple and precise. Keep it up <3 Respect <3

AliHamza-etsm
Автор

These videos are well put together well explained. Many thanks and keep up the good work!

casualSeth
Автор

Thanks man, this series helped me a lot ❤

ahmedmustafa
Автор

To prevent my test code from breaking the near future in case I made some changes to the Factory, I used ```expect { delete '/api/v1/books/:id' }.to change { Book.count }.by(-1) ```.
This only checks if the record was reduced by 1 instead of checking a change from a specific value to another.

jelilabudu
Автор

Great video! The way you change the code only after a great explanation is nice! You save me a lot of time! I've already subscribed me on your channel.

paulofelipesilvadesouza
Автор

damn that's exactly what i needed, thanks!

le-hu
Автор

Hi Tom, great video and series.
I was having trouble with Delete test and then figured that
***delete "/api/v1/books/***
was not working because on each rspec run the books ID was auto incrementing.


***book_id = Book.first.id***
***delete

So I wanted to know if I missed something or database somehow the database was being auto cleaned

nikgoy
Автор

Excellent series. Do you have the source code in github for reference?

Omar-ndlh
Автор

I also added the faker gem because i am also terrible at coming up with fake data

sharkofjoy
Автор

How would I do an edit?
describe 'PATCH /books/:id' do
let!(:book) { FactoryBot.create(:book, author: 'George Orwell', title: '1984') }

it 'update a book' do
expect {
patch "/api/v1/books/#{book.id}", params: {book: {title: 'The Martian', author: 'Andy Weir'} }
}.to change { Book.count }.from(1).to(0)

expect(response).to have_http_status(:ok)
end
end

I do not know how to do

borisbonett