Skip to content

Commit e453a9f

Browse files
committed
Add model tests
1 parent f649eb0 commit e453a9f

File tree

4 files changed

+82
-13
lines changed

4 files changed

+82
-13
lines changed

app/models/pet.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class Pet < ApplicationRecord
3333
validates :weight, numericality: { greater_than: 0 }
3434

3535
def image_url(width: 100, height: 100)
36-
if pet_type == "dog"
37-
"https://placedog.net/#{width}/#{height}?id=#{id % 230}"
36+
if dog?
37+
"https://placedog.net/#{width}/#{height}?id=#{(id || 0) % 230}"
3838
else
39-
"https://placekitten.com/#{width}/#{height}?image=#{id % 16}"
39+
"https://placekitten.com/#{width}/#{height}?image=#{(id || 0) % 16}"
4040
end
4141
end
4242

@@ -53,18 +53,22 @@ def similiar_name_pets
5353
end
5454

5555
def tagline
56-
if pet_type == "dog"
56+
if dog?
5757
"#{name} is a #{age} year old #{breed} looking for a home!"
5858
else
5959
JSON.parse(Net::HTTP.get(URI("https://catfact.ninja/fact")))["fact"]
6060
end
6161
end
6262

6363
def promo
64-
if pet_type == "dog"
64+
if dog?
6565
Net::HTTP.get(URI("http://localhost:3000/mock/slow-service"))
6666
else
6767
Net::HTTP.get(URI("http://localhost:3000/mock/outlier/#{id}"))
6868
end
6969
end
70+
71+
def dog?
72+
pet_type == "dog"
73+
end
7074
end

spec/models/pet_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Pet, type: :model do
4+
describe "validations" do
5+
it "requires a name" do
6+
pet = Pet.new
7+
pet.valid?
8+
expect(pet.errors.messages).to have_key(:name)
9+
end
10+
11+
it "requires an age" do
12+
pet = Pet.new
13+
pet.valid?
14+
expect(pet.errors.messages).to have_key(:age)
15+
end
16+
17+
it "requires a breed" do
18+
pet = Pet.new
19+
pet.valid?
20+
expect(pet.errors.messages).to have_key(:breed)
21+
end
22+
23+
it "requires a color" do
24+
pet = Pet.new
25+
pet.valid?
26+
expect(pet.errors.messages).to have_key(:color)
27+
end
28+
29+
it "requires a weight" do
30+
pet = Pet.new
31+
pet.valid?
32+
expect(pet.errors.messages).to have_key(:weight)
33+
end
34+
35+
it "requires a weight greater than 0" do
36+
pet = Pet.new(weight: 0)
37+
pet.valid?
38+
expect(pet.errors.messages).to have_key(:weight)
39+
end
40+
end
41+
42+
describe "methods" do
43+
describe "#image_url" do
44+
it "returns a dog image url for dogs" do
45+
pet = Pet.new(pet_type: "dog")
46+
expect(pet.image_url).to include("https://placedog.net")
47+
end
48+
49+
it "returns a cat image url for cats" do
50+
pet = Pet.new(pet_type: "cat")
51+
expect(pet.image_url).to include("https://placekitten.com")
52+
end
53+
end
54+
55+
describe "#tagline" do
56+
it "returns a dog tagline for dogs" do
57+
pet = Pet.new(pet_type: "dog", name: "Fido", age: 5, breed: "Golden Retriever")
58+
expect(pet.tagline).to eq("Fido is a 5 year old Golden Retriever looking for a home!")
59+
end
60+
61+
it "returns a cat fact for cats" do
62+
allow(Net::HTTP).to receive(:get).and_return('{"fact": "Cats are cool"}')
63+
pet = Pet.new(pet_type: "cat")
64+
expect(pet.tagline).to eq("Cats are cool")
65+
end
66+
end
67+
end
68+
end

spec/models/pet_view_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
require 'rails_helper'
22

33
RSpec.describe PetView, type: :model do
4-
pending "add some examples to (or delete) #{__FILE__}"
4+
it "validates presence of pet" do
5+
pet_view = PetView.new
6+
pet_view.valid?
7+
expect(pet_view.errors.messages).to have_key(:pet)
8+
end
59
end

spec/requests/mock_spec.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)