0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイル構造

Last updated at Posted at 2025-01-14

ファイル構造

The directory structure

Specsは普段目的を説明する正規ディレクトリ構造に配置される

Specs are usually placed in a canonical directory structure that describes their purpose:

モデル仕様書はspec/modelsディレクトリに属する

  • Model specs reside in the spec/models directory
  • Controller specs reside in the spec/controllers directory

Request仕様書はspec/requestsディレクトリに属される。ディレクトリはapiまたはインテグレーションと名付けることもできる。

  • Request specs reside in the spec/requests directory. The directory can also be named integration or api.
  • Feature specs reside in the spec/features directory
  • View specs reside in the spec/views directory
  • Helper specs reside in the spec/helpers directory
  • Mailer specs reside in the spec/mailers directory
  • Routing specs reside in the spec/routing directory
  • Job specs reside in the spec/jobs directory
  • System specs reside in the spec/system directory

アプリ開発者は異なるディレクトリ構造を使うため自由に使用することができる。正確なrspec-railsサポート機能をインクルードするため、仕様書は適切で一致するメタデータ:type 値:を持つことが必要です

Application developers are free to use a different directory structure. In order to include the correct rspec-rails support functions, the specs need to have the appropriate corresponding metadata :type value:

モデルの仕様書は type::model

  • Model specs: type: :model
  • Controller specs: type: :controller
  • Request specs: type: :request
  • Feature specs: type: :feature
  • View specs: type: :view
  • Helper specs: type: :helper
  • Mailer specs: type: :mailer
  • Routing specs: type: :routing
  • Job specs: type: :job
  • System specs: type: :system

例えば、ThingsControllerに対する仕様がspec/legacy/things_controller_spec.rbに置かれているとします。簡単に仕様書のRSpec.describeブロックにtype: :controllerメタデータをタグ付けする。

For example, say the spec for the ThingsController is located in spec/legacy/things_controller_spec.rb. Simply tag the spec’s RSpec.describe block with the type: :controller metadata:

  # spec/legacy/things_controller_spec.rb
  RSpec.describe ThingsController, type: :controller do #<-type: :controllerメタデータのタグがついている
    describe "GET index" do
      # Examples
    end
  end

注意:標準的なRSpecの仕様書は初期値によっていくつかの追加メタデータを必要としない

Note: Standard RSpec specs do not require any additional metadata by default.

詳細ついては、メタデータの使用するにはrspec-coreドキュメントを確かめて下さい

Check out the rspec-core documentation on using metadata for more details.

自動的にメタデータの追加

Automatically Adding Metadata

3.0.0より前のRSpecバージョンは自動的にファイルシステム上の位置に基づいて仕様書にメタデータを自動的に追加するこれは新しいユーザーも混乱させ、ベテランのユーザーに対しても望ましくない。

RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. This was both confusing to new users and not desirable for some veteran users.

この振る舞いは明示的に有効にしなければならない

This behaviour must be explicitly enabled:

  # spec/rails_helper.rb
  RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
  end

想定された振る舞いはチュートリアルでとても一般的に行われているので、rails generate rspec:installで生成された初期の構成はこれが可能である。

Since this assumed behavior is so prevalent in tutorials, the default configuration generated by rails generate rspec:install enables this.

もし上のリスト化された正規ディレクトリ構成に従い、infer_spec_type_from_file_location!を構成したのならば、RSpecはそれぞれのタイプに対して正確なサポート機能を自動的に含むだろう。

If you follow the above listed canonical directory structure and have configured infer_spec_type_from_file_location!, RSpec will automatically include the correct support functions for each type.

上記の正規構造に合わないカスタム・ディレクトリにメタデータを設定したい場合、次のことができる。

If you want to set metadata for a custom directory that doesn’t follow fit the canonical structure above, you can do the following:

  # set `:type` for serializers directory
  RSpec.configure do |config|
    config.define_derived_metadata(:file_path => Regexp.new('/spec/serializers/')) do |metadata|
      metadata[:type] = :serializer
    end
  end

仕様書の位置のコツ

Tips on Spec Location

sepc/ディレクトリ構造は一般的にapp/lib/を両方反映することを提案する。これにより対応するコードとspecファイルを簡単に確認できる。

It is suggested that the spec/ directory structure generally mirror both app/ and lib/. This makes it easy to locate corresponding code and spec files.

標準的なrailsの仕様書は:typeメタデータを明示しなければならない

Standard Rails specs must specify the :type metadata

require "rails_helper"

RSpec.describe WidgetsController, type: :controller do
  it "responds successfully" do
    get :index                         # httpリクエストも遅れる
    expect(response.status).to eq(200) # httpステータスも検査することができるらしい
  end
end

railsと関係ない仕様書は初期値によって:typeメタデータを必要としない

Non-rails related specs do not require :type metadata by default

require "spec_helper"

Entry = Struct.new(:description, :us_cents)

RSpec.describe Entry do
  it "has a description" do
    is_expected.to respond_to(:description) # 
  end
end

ファイルの位置から仕様書のタイプを推測し、適切なメタデータを加える

Inferring spec type from the file location adds the appropriate metadata

require "rails_helper"

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

RSpec.describe WidgetsController do
  it "responds successfully" do
    get :index
    expect(response.status).to eq(200)
  end
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?