Skip to content
Permalink
Browse files

Create console model for Episode 04

This commit includes all of the changes for Episode 04, Unit Testing
Models.
  • Loading branch information...
brettchalupa committed Nov 11, 2017
1 parent 32bed7d commit 9e7a1eefde159b8089f47a322653178f45cb0e46
@@ -1,22 +1,11 @@
class ConsolesController < ApplicationController
def index
consoles = [
{ name: 'NES', manufacturer: 'Nintendo' },
{ name: 'SNES', manufacturer: 'Nintendo' },
{ name: 'Wii', manufacturer: 'Nintendo' },
{ name: 'Genesis', manufacturer: 'Sega' },
{ name: 'Xbox', manufacturer: 'Microsoft' },
{ name: 'Switch', manufacturer: 'Nintendo' },
{ name: 'PS1', manufacturer: 'Sony' },
{ name: 'PS2', manufacturer: 'Sony' }
]

if params[:manufacturer].present?
consoles = consoles.select do |console|
console[:manufacturer] == params[:manufacturer]
end
consoles = Console.where(manufacturer: params[:manufacturer])
else
consoles = Console.all
end

render(json: { 'consoles' => consoles.map { |console| console[:name] }})
render(json: { 'consoles' => consoles.map(&:formatted_name) })
end
end
@@ -0,0 +1,10 @@
class Console < ActiveRecord::Base
validates :name, presence: true
validates :manufacturer, presence: true

scope :nintendo, -> { where(manufacturer: 'Nintendo') }

def formatted_name
"#{manufacturer} #{name}"
end
end
@@ -0,0 +1,10 @@
class CreateConsoles < ActiveRecord::Migration[5.1]
def change
create_table :consoles do |t|
t.string :name, null: false
t.string :manufacturer, null: false

t.timestamps
end
end
end
@@ -10,6 +10,13 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 20171111171300) do

create_table "consoles", force: :cascade do |t|
t.string "name", null: false
t.string "manufacturer", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe Console do
subject { described_class.new(manufacturer: 'Nintendo', name: 'Wii') }

describe 'validations' do
describe 'name' do
it 'must be present' do
expect(subject).to be_valid
subject.name = nil
expect(subject).to_not be_valid
end
end

describe 'manufacturer' do
it 'must be present' do
expect(subject).to be_valid
subject.manufacturer = nil
expect(subject).to_not be_valid
end
end
end

describe '#formatted_name' do
it 'returns the manufacturer and name in a string' do
expect(subject.formatted_name).to eql('Nintendo Wii')
end
end

describe '.nintendo' do
it 'returns an ActiveRecord::Relation of consoles manufacturered by Nintendo' do
wii = described_class.create(manufacturer: 'Nintendo', name: 'Wii')
switch = described_class.create(manufacturer: 'Nintendo', name: 'Switch')
described_class.create(manufacturer: 'Sony', name: 'PS4')

expect(described_class.nintendo).to contain_exactly(
wii,
switch
)
end
end
end
@@ -1,28 +1,39 @@
require 'rails_helper'

RSpec.describe 'Consoles requests' do
before do
Console.create(name: 'NES', manufacturer: 'Nintendo')
Console.create(name: 'SNES', manufacturer: 'Nintendo')
Console.create(name: 'Wii', manufacturer: 'Nintendo')
Console.create(name: 'Genesis', manufacturer: 'Sega')
Console.create(name: 'Xbox', manufacturer: 'Microsoft')
Console.create(name: 'Switch', manufacturer: 'Nintendo')
Console.create(name: 'PS1', manufacturer: 'Sony')
Console.create(name: 'PS2', manufacturer: 'Sony')
end

describe 'GET /consoles' do
it 'returns an array of the consoles' do
get('/consoles')
expect(response_json['consoles']).to contain_exactly(
'NES',
'SNES',
'Wii',
'Genesis',
'Xbox',
'Switch',
'PS1',
'PS2'
'Nintendo NES',
'Nintendo SNES',
'Nintendo Wii',
'Sega Genesis',
'Microsoft Xbox',
'Nintendo Switch',
'Sony PS1',
'Sony PS2'
)
end

it 'supports specifying consoles for a specific manufacturer' do
get('/consoles', params: { manufacturer: 'Nintendo' })
expect(response_json['consoles']).to contain_exactly(
'NES',
'SNES',
'Wii',
'Switch'
'Nintendo NES',
'Nintendo SNES',
'Nintendo Wii',
'Nintendo Switch'
)
end
end

0 comments on commit 9e7a1ee

Please sign in to comment.
You can’t perform that action at this time.