Skip to content

Commit

Permalink
test: write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoGabriel55 committed Dec 4, 2023
1 parent b1e0865 commit dbf076c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
36 changes: 36 additions & 0 deletions backend/spec/requests/movies/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "rails_helper"

RSpec.describe "Movies", type: :request do
describe "DELETE /api/movies/:id" do
context "when movie exists" do
let!(:movie) {
Movies::Create.new(
params: {
director: "George Lucas",
writer: "George Lucas",
title: "Star Wars",
producer: "George Lucas",
production_company: "Lucasfilm",
cast: ["Han Solo", "Princess Leia"],
year: 1977
}
).call
}

it "responds with 204" do
expect { delete "/api/movies/#{movie.id}" }
.to change { MoviesRepository.count }.by(-1)

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

context "when movie does not exist" do
it "responds with 204" do
delete "/api/movies/123"

expect(response).to have_http_status(:no_content)
end
end
end
end
43 changes: 43 additions & 0 deletions backend/spec/requests/movies/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "rails_helper"

RSpec.describe "Movies", type: :request do
let(:response_json) { JSON.parse(response.body) }

describe "GET /api/movies/:id" do
context "when there is a movie" do
let!(:game) {
Movies::Create.new(
params: {
director: "George Lucas",
writer: "George Lucas",
title: "Star Wars",
producer: "George Lucas",
production_company: "Lucasfilm",
cast: ["Han Solo", "Princess Leia"],
year: 1977
}
).call
}

it "responds with a movie" do
get "/api/movies/#{game.id}"

expect(response_json).to include({
"title" => "Star Wars",
"cast" => ["Han Solo", "Princess Leia"],
"year" => 1977
})
expect(response).to have_http_status(:ok)
end
end

context "when there is no movie" do
it "responds with an error" do
get "/api/movies/1"

expect(response_json).to eq({ "error" => "Movie not found" })
expect(response).to have_http_status(:not_found)
end
end
end
end
45 changes: 45 additions & 0 deletions backend/spec/requests/movies/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "rails_helper"

RSpec.describe "Movies", type: :request do
let(:response_json) { JSON.parse(response.body) }

let(:params) do
{
"movie" => {
"title": "Star Wars 2"
}
}
end

describe "PATCH /api/movies/:id" do
context "when movie exists" do
let!(:movie) {
Movies::Create.new(
params: {
director: "George Lucas",
writer: "George Lucas",
title: "Star Wars",
producer: "George Lucas",
production_company: "Lucasfilm",
cast: ["Han Solo", "Princess Leia"],
year: 1977
}
).call
}

it "responds with 200" do
patch "/api/movies/#{movie.id}", params: params

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

context "when movie does not exist" do
it "responds with 404" do
patch "/api/movies/1", params: params

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

0 comments on commit dbf076c

Please sign in to comment.