Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cm/wave 1 controller tests #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## At a Glance
- Pair, [stage 2](https://github.com/Ada-Developers-Academy/pedagogy/blob/master/rule-of-three.md#stage-2) project
- Due EOD Friday at 6 PM on **DATE HERE**
- Due EOD Friday at 6 PM on **EOD Friday December 4th**
- Submit this project with a PR

## Introduction
Expand Down
Empty file added test/controllers/.keep
Empty file.
36 changes: 36 additions & 0 deletions test/controllers/customers_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

describe CustomersController do
it "must get index" do
# Act
get customers_path
body = JSON.parse(response.body)

# Assert
expect(body).must_be_instance_of Array
expect(body.length).must_equal Customer.count

# Check that each customer has the proper keys
fields = ["id", "name", "registered_at", "postal_code",
"phone", "videos_checked_out_count"].sort

body.each do |customer|
expect(customer.keys.sort).must_equal fields
end

must_respond_with :ok
end

it "works even with no customers" do
Customer.destroy_all

get customers_path
body = JSON.parse(response.body)

expect(body).must_be_instance_of Array
expect(body.length).must_equal 0

must_respond_with :ok
end

end
117 changes: 117 additions & 0 deletions test/controllers/videos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require "test_helper"

describe VideosController do
describe "index" do
it "must get index" do
# Act
get videos_path
body = JSON.parse(response.body)

# Assert
expect(body).must_be_instance_of Array
expect(body.length).must_equal Video.count

# Check that each customer has the proper keys
fields = ["id", "title", "release_date", "available_inventory"].sort

body.each do |customer|
expect(customer.keys.sort).must_equal fields
end

must_respond_with :ok
end

it "works even with no videos" do
# Arrange
Video.destroy_all

# Act
get videos_path
body = JSON.parse(response.body)

# Assert
expect(body).must_be_instance_of Array
expect(body.length).must_equal 0

must_respond_with :ok
end
end

describe "show" do
it "can get a video" do
# Arrange
wonder_woman = videos(:wonder_woman)

# Act
get video_path(wonder_woman.id)
body = JSON.parse(response.body)

# Assert
fields = ["title", "overview", "release_date", "total_inventory", "available_inventory"].sort
expect(body.keys.sort).must_equal fields
expect(body["title"]).must_equal "Wonder Woman 2"
expect(body["release_date"]).must_equal "December 25th 2020"
expect(body["available_inventory"]).must_equal 100
expect(body["overview"]).must_equal "Wonder Woman squares off against Maxwell Lord and the Cheetah, a villainess who possesses superhuman strength and agility."
expect(body["total_inventory"]).must_equal 100

must_respond_with :ok
end

it "responds with a 404 for non-existant videos" do
# Act
get video_path(-1)
body = JSON.parse(response.body)

# Assert
expect(body.keys).must_include "errors"
expect(body["errors"]).must_include "Not Found"
must_respond_with :not_found
end
end

describe "create" do
it "can create a valid video" do
# Arrange
video_hash = {
title: "Alf the movie",
overview: "The most early 90s movie of all time",
release_date: "December 16th 2025",
total_inventory: 6,
available_inventory: 6
}

# Assert
expect {
post videos_path, params: video_hash
}.must_change "Video.count", 1

must_respond_with :created
end

it "will respond with bad request and errors for an invalid movie" do
# Arrange
video_hash = {
title: "Alf the movie",
overview: "The most early 90s movie of all time",
release_date: "December 16th 2025",
total_inventory: 6,
available_inventory: 6
}

video_hash[:title] = nil

# Assert
expect {
post videos_path, params: video_hash
}.wont_change "Video.count"
body = JSON.parse(response.body)

expect(body.keys).must_include "errors"
expect(body["errors"].keys).must_include "title"
expect(body["errors"]["title"]).must_include "can't be blank"

must_respond_with :bad_request
end
end
end
Empty file added test/fixtures/.keep
Empty file.
18 changes: 18 additions & 0 deletions test/fixtures/customers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
customer_one:
name: Simon Del Rosario
registered_at: "Wed, 29 Apr 2015 07:54:14 -0700"
postal_code: 75007
phone: (469) 734-9111
videos_checked_out_count: 3
address: 1314 Elm Street
city: Seattle
state: WA
customer_two:
name: Becca
registered_at: "Wed, 13 Mar 2020 07:54:14 -0700"
postal_code: 98177
phone: (469) 734-9222
videos_checked_out_count: 1
address: 1414 Seasame Street
city: Seattle
state: WA
Empty file added test/fixtures/files/.keep
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/videos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
wonder_woman:
title: Wonder Woman 2
release_date: "December 25th 2020"
available_inventory: 100
overview: Wonder Woman squares off against Maxwell Lord and the Cheetah, a villainess who possesses superhuman strength and agility.
total_inventory: 100
black_widow:
title: Black Widow
release_date: "May 7th 2021"
available_inventory: 6
overview: At birth the Black Widow (aka Natasha Romanova) is given to the KGB, which grooms her to become its ultimate operative. When the U.S.S.R. breaks up, the government tries to kill her as the action moves to present-day New York, where she is a freelance operative.
total_inventory: 7