Skip to content

Commit e6962bd

Browse files
author
Eugeny Khlopin
committed
Add books request tests
1 parent 9ea97a6 commit e6962bd

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/tmp/*
2+
.byebug_history
3+
/config/database.yml
4+
/coverage/*
5+
/log/*

spec/factories/authors.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :author do
3+
name { Faker::Book.author }
4+
end
5+
end

spec/factories/books.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :book do
3+
title { Faker::Book.title }
4+
description { Faker::ChuckNorris.fact }
5+
end
6+
end

spec/request/api/v1/books_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'Book', type: :request do
4+
5+
let!(:books) { FactoryBot.create_list(:book, 10) }
6+
7+
let(:valid_params) do
8+
{
9+
book: {
10+
title: Faker::Book.title,
11+
destcription: Faker::ChuckNorris.fact
12+
}
13+
}
14+
end
15+
16+
describe 'GET /books/' do
17+
it 'Gets books' do
18+
get '/api/v1/books'
19+
expect(response).to be_success
20+
expect(json.count).to eq(books.count)
21+
end
22+
end
23+
24+
describe 'GET /books/:id' do
25+
it 'Show the book by id' do
26+
book = books.sample
27+
get "/api/v1/books/#{book.id}"
28+
29+
expect(response).to be_success
30+
expect(json['id']).to eq(book.id)
31+
end
32+
end
33+
34+
describe 'DELETE /books/:id' do
35+
it 'Deletes the book by id' do
36+
book = books.sample
37+
delete "/api/v1/books/#{book.id}"
38+
expect(response).to be_success
39+
expect(Book.find_by(id: book.id)).to eq(nil)
40+
end
41+
end
42+
43+
describe 'POST /books/' do
44+
it 'Create a book' do
45+
post '/api/v1/books', params: valid_params
46+
expect(response).to be_success
47+
expect(json).to have_key('id')
48+
expect(books.count).to be < Book.count
49+
end
50+
end
51+
52+
describe 'PUT /books/:id' do
53+
it 'Update the book' do
54+
book = books.sample
55+
params = { book: { title: book.title.upcase } }
56+
put "/api/v1/books/#{book.id}", params: params
57+
expect(response).to be_success
58+
expect(json['title']).to eq(book.title.upcase)
59+
end
60+
end
61+
62+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module RequestSpecHelper
22
def json
3-
JSON.parse(response_body)
3+
JSON.parse(response.body)
44
end
55
end

0 commit comments

Comments
 (0)