LoginSignup
0
1

More than 1 year has passed since last update.

Rspecでモデルテストを書く

Posted at

Rspec初心者の備忘録の記事です。
Rspecでテストを書くためのセットアップや、基本となるモデルのバリデーションテストを記述していきます。

Rspecのインストール

.Gemfile
group :development, :test do
# 省略
gem 'rspec-rails' end
$ bundle install
config/database.yml
test:
<<: *default
database: happylunch_test

もしテストデータベースをまだ作っていないなら、下記を実行しましょう。すでに作成 済みなら、rails タスクはテストデータベースがすでにあることを教えてくれます。既存のデータベースを間違って消してしまう心配はありません。

$ rails db:create:all
This task only modifies local databases. happylunch_development is on a remote host.
This task only modifies local databases. happylunch_test is on a remote host.
This task only modifies local databases. happylunch_production is on a remote host.

では RSpec 自体の設定に進みます。

次のようなコマンドを使って RSpec をインストールしましょう。

$ rails generate rspec:install
Running via Spring preloader in process 325
      create  .rspec
       exist  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

次に、必須ではありませんが、RSpec の出力をデフォルトの形式から読みやすいドキ ュメント形式に変更します。これによってテストスイートの実行中にどのスペッ クがパスし、どのスペックが失敗したのかがわかりやすくなります。先ほど作成された .rspec ファイルを開き、以下のように変更します。

.rspec
--require spec_helper
--format documentation
config/application.rb
# 省略

module Projects
class Application < Rails::Application
    config.load_defaults 7.0

    config.generators do |g| 
      g.test_framework :rspec,
        fixtures: false, 
        view_specs: false, 
        helper_specs: false, 
        routing_specs: false
        g.factory_bot false
    end
  end 
end

g.factory_bot falseはFactory Bot のジェネレータを一時的に無効化 するためです。

モデルスペック

最初 のモデルスペックを作成するため、この作業の出発地点となるファイルを実際に生成します。
まず、rspec:model ジェネレータをコマンドラインから実行します。

するとPuser_spec.rbファイルが作成されるので、まずは基本となるバリデーションテストを書いてみます。

$ rails g rspec:model user
user_spec.rb
require 'rails_helper'

RSpec.describe User, type: :model do
  # 名前、メール、パスワードがあれば有効であること
  it "is valid with a name, email, and password" do
    user = User.new(
      name:  "Ace",
      email:      "tester@example.com",
      password:   "dottle-nouveau-pavilion-tights-furze",
    )
    expect(user).to be_valid
  end
  # 名前がなければ無効であること
  it "is invalid without a name" do
    @user = User.new(name: nil)
    expect(@user.valid?).to eq(false)
  end
  # メールアドレスがなければ無効であること
  it "is invalid without an email address" do
    @user = User.new(email: nil)
    expect(@user.valid?).to eq(false)
  end
  # 重複したメールアドレスなら無効であること
  it "is invalid with a duplicate email address" do
    User.create(
      name: "John",
      email:      "test@example.com",
      password:   "i-am-john"
    )
    @user = User.new( 
      name: "Peter", 
      email:      "test@example.com",
      password:   "i-am-peter"
    ) 
    @user.valid?
    expect(@user.valid?).to eq(false)
  end
end
$ bundle exec rspec

テストがパスします。

以降のモデルテストやコントローラテストはまた次の記事で書きます!

参考:
Everyday Rails - RSpecによるRailsテスト入門

0
1
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
1