【六日目】ChatSpace テストコード
RSpecを使ったテストについて学んだことの忘備録。
1.まずは、テストで使うGem導入
・コントローラのテストのため、'rails-controller-testing'
・ダミーインスタンス作成のため、'factory_bot_rails'
・ダミーデータ作成のため、'faker'
をGemfileに追加。
忘れずに、bundle installでインストール。
Gemfile
1 2 3 4 5 6 7 8 |
2.導入したrspecをインストール。
出力を見やすくするため追加。
1 |
--format documentation
|
これでテストの準備完了。
3.FactoryBot使用
まずは、FactroyBotの記述を省略するためのコード。
/spec/rails_helper.rb
1 2 3 |
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
|
spec/factories/users.rb
1 2 3 4 5 6 7 8 9 |
FactoryBot.define do
factory :user do
password = Faker::Internet.password(8)
name {Faker::Name.last_name}
email {Faker::Internet.free_email}
password {password}
password_confirmation {password}
end
end
|
続きは今度。