LoginSignup
0
0

More than 1 year has passed since last update.

[Rspec][Rails] publicなどの名前空間を使用した際のファイル命名方法

Last updated at Posted at 2022-05-21

はじめに

すみません、ディレクトリ構成に誤りがあったので少し修正してあります!

管理者側(admin)とユーザー側(public)を持つアプリケーションを作成中です。
Rspecを触り始めて2日目、 名前空間を使ったフォルダ・ファイルの命名規則で思いっきり躓いたので投稿します。
情報がほとんど無いため、requests(コントローラー)のテストで特に躓きました。

たまたま見つけられたこれから参考になりそうなGitHub

(GitHubなので少し迷いましたが公開しているならいいのかと思い載せました、もし何かあれば教えて下さい。)

結合テストのとても参考にさせていただきました!

環境

Rails 6.1.5.1
ruby 2.6.3
RSpec 3.11

結論(間違っていたので修正済み)

public/users_controller.rb なのであれば

public_users_spec.rbでいいみたいです!

requestsもsystemも基本コントローラーと同じディレクトリ構成で作成するようです!

spec/requests/public/users_spec.rbということでした!

※厳密に言うとsystemは決まり的なものはないらしいのでわかりやすい構成でいいそうです。
※モデルのテストは違います

モデルのテストなら

spec/models/user_spec.rb

モデルはそもそも名前空間とか関係なく一つしか作らないのでシンプルです。
注意点としてはmodelsなことですかね?
userは単数形です。

中身少しだけ
models/user_spec.rb
require 'rails_helper'

RSpec.describe 'ユーザーモデル', type: :model do

  describe 'モデルに関するテスト' do
    let(:user) { create(:user) }
    let(:other_user) { build(:user, :other_user) }
    it 'name, email, password, encrypted_passwordがあれば有効なこと' do
      expect(user).to be_valid
    end
  end
end

requests(コントローラー)のテストなら

spec/requests/public/users_spec.rb

ここですね!
名前空間_コントローラー名_spec.rbです。

たまたま見かけたサイトでは(見間違いかもしれませんが)_spec.rbのところを_request.rbと書いてあったりして混乱しました。

中身少しだけ
requests/public/users_spec.rb
require 'rails_helper'

RSpec.describe 'Postsコントローラー', type: :request do
  let!(:user) { create(:user) }
  let!(:post) { create(:post, user_id: user.id) }
  describe "newのアクション" do
    it "returns http success" do
      sign_in user
      get new_post_path
      expect(response).to have_http_status(:success)
    end
  end
end

※これは冒頭に載せたGitHubから動作の確認のためにテスト内容を拝借しました。普通は日本語か英語か統一して書きます。

system(結合)のテストなら

spec/system/public/users_spec.rb

ここはsystemらしいです。
後ろはrequestsと同じですね。

一部ですがここだけ長いです
system/public/users_spec.rb
require 'rails_helper'

RSpec.describe 'ユーザー新規登録', type: :system do
  let(:user) { build(:user) }
  let(:other_user) { create(:user, :other_user) }
  let(:post) { build(:post, user_id: user.id) }
  describe 'ログイン前' do
    describe '新規登録' do
      it '正しい情報を入力すればユーザー新規登録が出来てuser_pathに移動する' do
        # トップページに移動
        visit root_path
        # トップページにサインアップへ遷移する新規登録ボタンがあることを確認する
        expect(page).to have_content('新規登録')
        # 新規登録ページへ移動
        visit new_user_registration_path
        # ユーザー情報を入力
        fill_in 'user_name', with: user.name
        fill_in 'user_email', with: user.email
        fill_in 'user_password', with: user.password
        fill_in 'user_password_confirmation', with: user.encrypted_password
        # 新規登録ボタンを押すとユーザーモデルのカウントが1上がることを確認する
        expect{
          click_button('新規登録')
        }.to change { User.count }.by(1)
        # user_pathへ遷移する
        expect(page).to have_current_path user_path(1)
        # マイページの表示がある
        expect(page).to have_content('マイページ')
        # ログアウトボタンが表示されている事を確認
        expect(page).to have_content('ログアウト')
        # 新規登録ボタンやログインボタンが無いことを確認する
        expect(page).to_not have_content('ログイン')
        expect(page).to_not have_content('新規登録')
      end
       end
  end
end

最後に

まだ始めたばかりで現状はこれで動いているといった形なので何かあれば教えて下さい。

案外Rspecの情報が少なく、しかも古いのもたくさんあって情報収集に苦戦していますが頑張っていきます!

閲覧頂きましてありがとうございました!🐎

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