# 20220819 測試 ###### tags: `Ruby` `Rails` `RSPEC` ## 搜尋bar 好空~~~ 請看回放~~~ ## test 通常寫功能前就要先測試 ### 測試套件 rspec https://rubygems.org/gems/rspec-rails/versions/3.4.2 https://github.com/rspec/rspec-rails https://ithelp.ithome.com.tw/users/20140124/ironman/4239 **寫測試必背三步驟:** - Arrange - Act - Assert ### factory pattern 工廠模式 [factory bot](https://github.com/thoughtbot/factory_bot) <- 點我 - 自動產生對應的資料(user)可以在測試時使用,不用每次都自己生 進入測試環境的rails console ```shell! RAILS_ENV = TEST rails c --sandbox ``` factory 裡面的寫法 ```ruby! factory :user do email { Faker::Internet.email } password { Faker::Internet.password } end ``` ```ruby! factory :article do title { Faker::Lorem.sentence } content { Faker::Lorem.paragraph } # user上面已經有寫好factory 會自己去找 user end ``` 題外話: 問面試官問題 版本控制的流程? 有沒有在寫測試? ## Tag 一個表單用兩個 model 巢狀選項 Active Record Nested Attributes https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html ```ruby! <%= form fields_for :tags do |x| %> ``` 拖拉keyword: drag and drop js npm套件(前端) https://www.npmjs.com/package/sortablejs 後端,記錄拖拉後的新位置 https://github.com/brendon/acts_as_list ### 做標籤 https://github.com/mbleigh/acts-as-taggable-on ### Tailwind SCSS syntax 針對不同輸入框 ```scss! input[type="...."],[type="...."]{ @apply ........... } ``` 要自訂css樣式 -> 另外拉出來寫 ```scss! .名稱 { @apply css樣式 } ``` ### polymorphic rails [polymorphic](https://zh.wikipedia.org/zh-tw/%E5%A4%9A%E6%80%81_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6)) 更有彈性的多對多。 讓要參與的model變成一種商品, 多型關連(Polymorphic Associations)可以讓一個 Model 不一定關連到某一個特定的 Model,秘訣在於除了整數的_id外部鍵之外,再加一個字串的_type欄位說明是哪一種Model。 例如一個Comment model,我們可以透過多型關連讓它belongs_to到各種不同的 Model上,假設我們已經有了Article與Photo這兩個Model,然後我們希望這兩個Model都可以被留言。不用多型關連的話,你得分別建立ArticleComment和PhotoComment的model。用多型關連的話,無論有多少種需要被留言的Model,只需要一個Comment model即可 參考資料:https://ihower.tw/rails/activerecord-relationships.html 畫圖會更清楚~~ ### Counter Cache -> Tag cloud 先修教材 -> *3.9 純手工打造 015* 跟效能有關 ### 視訊語音 stream 先拿到鏡頭跟音訊的權限(前端) => javascript carmera MediaDevice 拿鏡頭這件事本身就不難,難在你要怎麼把畫面傳到對方的電腦上。 需要透過一個媒介~~~~~ ### 第三方登入 keyword: oauth2 [ngrok](https://ngrok.com/) 開發的時候,很好用~ ## crontab https://free.com.tw/crontab-guru/ gem [whenever](https://github.com/javan/whenever) gem "xxx", request:false ## chart.js jsonb rails new 專案名字 -d 資料庫名稱
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up