0
0

More than 3 years have passed since last update.

Rspec 結合テストコードにてセレクトボックスがある場合の記述方法

Last updated at Posted at 2021-02-27

テックキャンプではカリキュラム外であったために自分で調査

※セレクトボックスの結合テストのみの解説なので、それ以外は省略

実行コマンド

bundle exec rspec spec/system/users_spec.rb

アプリ名/spec/system/users_spec.rb



require 'rails_helper'

RSpec.describe 'ユーザー新規登録', type: :system do
  before do
    @user = FactoryBot.build(:user)
  end

  context 'ユーザー新規登録ができるとき' do
    it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do
      fill_in 'nickname', with: @user.nickname
#⇐この行----------------------------------------------------------
      select '男性', from: 'user[seibetu_id]'    
#---------------------------------------------------------------
    end
  end
end

アプリ名/spec/factories/users.rb


FactoryBot.define do
  factory :user do
    nickname              { Faker::Name.initials(number: 10) }
    seibetu_id            { 2 }
  end
end

アプリ名/app/models/seibetu.rb

class Seibetu < ActiveHash::Base
  self.data = [
    { id: 1, name: '--' },
    { id: 2, name: '男性' },
    { id: 3, name: '女性' },
    { id: 4, name: 'ゲイ' },
    { id: 5, name: 'レズ' },
    { id: 6, name: '性別は答えたくない' }
  ]

  include ActiveHash::Associations
  has_many :users
  has_many :loves
end

Gemfile


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]

  gem 'rspec-rails', '~> 4.0.0'
  gem 'factory_bot_rails'
  gem 'faker'
end

group :development do

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end


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