0
0

More than 3 years have passed since last update.

repec 備忘録

Posted at

導入方法

Gemfile
group :development, :test do
  gem 'rspec-rails' #追加
  gem 'capybara' #追加
#元あるやつはコメントアウトする
end
ターミナル
$ bundle install
$ rails generate rspec:install
spec/spec_helper.rb
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'capybara/rspec' #追記
RSpec.configure do |config|
  config.before(:each, type: :system) do #追記
    driven_by :rack_test #追記
  end #追記
.
.
.
config.include FactoryBot::Syntax::Methods 
#これを記入することでFactoryBot.create(:cake)⇒create(:cake)にできる
config.include CartItemTestHelper #(helperはここに追加)

初期設定

モデル、コントローラー作成時、関係するrspecのファイルを作成

config/application.rb
config.generators do |g|
  g.test_framework :rspec,
end

テスト実行画面をいい感じにする

.rspec
--require spec_helper
--color
--format d

spec/support/配下のファイルを読み込む
(コメントアウト外す)

/spec/rails_helper.rb
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

ファイル作成方法

ファクトリーボット

ターミナル
$ rails g factory_bot:model user

テストファイル

ターミナル
$ rails g rspec:system users
$ rails g rspec:model user

実行方法

ターミナル
#どちらかで起動可能
$ rspec
$ bundle exec rspec
#特定の箇所のみ
$ rspec spec/system/users_spec.rb
$ rspec spec/system/users_spec.rb:2

ファクトリーボット

表記方法
一意制約をもたせている場合は、email部のように表記する

FactoryBot.define do 
    factory :user do 
        name {"test"}
        sequence(:email) { |n| "test#{n}@example.com"}
        password {"password"}
    end 
end 

モデルのテスト

require 'rails_helper'

RSpec.describe CartItem, "モデルに関するテスト", type: :model do
  before do
    FactoryBot.create(:cake)
    FactoryBot.create(:candy)
    FactoryBot.create(:customer)
  end

  describe "実際に保存してみる" do
    it "有効な物は保存可能か?" do
      cart_item = CartItem.new(item_id: 1,customer_id: 1,amount: 2)
      expect(cart_item).to be_valid
    end

    context "空白のバリデーションチェック" do
      it "個数が空白" do
        cart_item = CartItem.new(item_id: 1,customer_id: 1,amount: "")
        expect(cart_item).to be_invalid
        expect(cart_item.errors[:amount]).to include("is not a number")
      end
      it "item_idが空白" do
        expect(FactoryBot.build(:cart_item, item_id: "")).to be_invalid
               #上記方法でもok
      end
    end
  end
end

フォームの入力

fill_in " idやname名 ", with: 入れたい内容
fill_in " idやname名 ", with: 入れたい内容
click_button " idやname名 "

fill_in "customer_email", with: customer.email
fill_in "customer_password", with: customer.password
click_button "commit"

helperの追加

helperの作成

spec/support/cart_item_test_helper.rb
module CartItemTestHelper
  def item_in_cart(item, amount)
    visit item_path(item)
    fill_in "cart_item_amount", with: amount
    click_button "commit"
  end
end

spec_helperに追記

spec/spec_helper.rb
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'capybara/rspec' 
RSpec.configure do |config|
  config.before(:each, type: :system) do 
    driven_by :rack_test 
  end 
.
.
.
config.include FactoryBot::Syntax::Methods 
config.include CartItemTestHelper #追記
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0