Current Sprint: 1. 完成 HappyPath: 可以建立一場遊戲,玩到結束
repo: https://github.com/side-project-at-SPT/ithome-ironman-2024-san-juan
swagger docs: https://side-project-at-spt.github.io/ithome-ironman-2024-san-juan/
經過一週之後,決定先放棄套方法論了 XDD
先按照直覺開發,後面再來看看有沒有辦法讓文章結構變得順暢
rails g games
# create app/controllers/games_controller.rb
# invoke rspec
# create spec/requests/games_spec.rb
games_spec.rb
require 'swagger_helper'
RSpec.describe "Api::V1::Games", type: :request do
path '/api/v1/games' do
get 'List all games' do
tags 'Games'
produces 'application/json'
response '200', 'Games found' do
schema type: :object,
properties: {
games: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :integer },
status: { type: :string }
},
required: [ 'id', 'status' ]
}
}
},
required: [ 'games' ]
run_test!
end
end
end
end
# config/routes.rb
# 加上 game route
Rails.application.routes.draw do
# ...
namespace :api do
namespace :v1 do
resources :games, only: [ :index ]
end
end
end
# app/controllers/api/v1/games_controller.rb
class Api::V1::GamesController < ApplicationController
def index
render json: { games: [ a: 1 ] }
end
end
rspec spec/requests/api/v1/games_spec.rb
# F
# Failures:
# 1) Api::V1::Games /api/v1/games get Games found returns a 200 response
# Failure/Error:
# raise UnexpectedResponse,
# "Expected response body to match schema: #{errors.join("\n")}\n" \
# "Response body: #{JSON.pretty_generate(JSON.parse(body))}"
# Rswag::Specs::UnexpectedResponse:
# Expected response body to match schema: The property '#/games/0' did not contain a required property of 'id' in schema 5c48736f-3746-5bb8-9970-fdb5b154e5fe
# The property '#/games/0' did not contain a required property of 'status' in schema 5c48736f-3746-5bb8-9970-fdb5b154e5fe
# Response body: {
# "games": [
# {
# "a": 1
# }
# ]
# }
# 建立 game model, 欄位 status type = integer, 並且如果沒宣告 id, 則會自動帶入 id = auto increment integer
rails g model game status:integer
# invoke active_record
# create db/migrate/20240908153434_create_games.rb
# create app/models/game.rb
# invoke rspec
# create spec/models/game_spec.rb
# 增加 null: false, default: 0 # 0: unknown, 1: playing, 2: finished
class CreateGames < ActiveRecord::Migration[7.2]
def change
create_table :games do |t|
t.integer :status, null: false, default: 0 # 0: unknown, 1: playing, 2: finished
t.timestamps
end
end
end
rails db:migrate
# == 20240908153434 CreateGames: migrating ======================================
# -- create_table(:games)
# -> 0.0117s
# == 20240908153434 CreateGames: migrated (0.0118s) =============================
# app/models/game.rb
class Game < ApplicationRecord
enum :status, {
unknown: 0,
playing: 1,
finished: 2
}, prefix: true
end
require 'swagger_helper'
RSpec.describe "Api::V1::Games", type: :request do
path '/api/v1/games' do
get 'List all games' do
tags 'Games'
produces 'application/json'
before do
Game.create
Game.create(status: 'playing')
end
response '200', 'Games found' do
schema type: :object,
properties: {
games: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :integer },
status: { type: :string }
},
required: [ 'id', 'status' ]
}
}
},
required: [ 'games' ]
run_test! do
json = JSON.parse(response.body)
expect(json['games'].size).to eq(2)
expect(json['games'].any? { |game| game['status'] == 'unknown' }).to be_truthy
expect(json['games'].any? { |game| game['status'] == 'playing' }).to be_truthy
end
end
end
end
end
rspec spec/requests/api/v1/games_spec.rb
# .
# Finished in 0.08544 seconds (files took 0.67108 seconds to load)
# 1 example, 0 failures
rspec spec/requests/api/v1/games_spec.rb
# F
# Failures:
# 1) Api::V1::Games /api/v1/games get Games found returns a 200 response
# Failure/Error: expect(json['games'].size).to eq(2)
# expected: 2
# got: 0
# (compared using ==)
# # ./spec/requests/api/v1/games_spec.rb:33:in `block (5 levels) in <top (required)>'
rails rswag
rails s
查看 swagger (go to localhost:3000/api-docs)
執行看看
完成了 0.5 XD
以上不代表明天會做,如有雷同純屬巧合
SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。
歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!
Discord頻道連結:https://sideproj.tw/dc