LoginSignup
123
139

More than 5 years have passed since last update.

Rails RSpecの基本 ~Model編~

Last updated at Posted at 2015-05-28

モデルスペックに含めるテスト内容

Everyday Rails - RSpecによるRailsテスト入門より

・有効な属性が渡された場合、モデルのcreateメソッドが正常に完了すること。
・バリデーションを失敗させるデータがあれば、正常に完了しないこと。
・クラスメソッドとインスタンスメソッドが期待通りに動作すること。

exampleを作成する

/spec/models/article_spec.rb
require 'rails_helper'

describe Article do
  #titleとcontentが入っていれば有効であること
  it "is valid with title and content"
end
実行結果
Article
  is valid with title and content (PENDING: Not yet implemented)

Pending:
  Article is valid with title and content
    # Not yet implemented
    # ./spec/models/article_spec.rb:5

Finished in 0.00056 seconds
1 example, 0 failures, 1 pending

require 'rails_helper'はすべてのスペックに含める。

マッチャを書いてテストをパスさせる

*マッチャ:期待値と実際の値を比較して、一致した(もしくは一致しなかった)という結果を返すオブジェクト。

/spec/models/article_spec.rb
 #省略
describe Article do
  #titleとcontentが入っていれば有効であること
  it "is valid with title and content" do
    article = Article.new(
      title: 'hoge',
      content: 'hogehoge'
    )
    expect(article).to be_valid
  end
end
実行結果
Article
  is valid with title and content

Finished in 0.01854 seconds
1 example, 0 failures

マッチャに関してはこちらを参考に。
使えるRSpec入門・その2「使用頻度の高いマッチャを使いこなす」

exampleを入れ子に記述する

*describeとcontextは同義。RSpecを出力したときに入れ子になって表示されてわかりやすくなる。

/spec/models/article_spec.rb
  describe "fetch recent article" do
    #articleの作成時間が指定時間内であれば情報を返すこと
    context "created_at > time" do
      it "returns recent_articles that match"
    end
    #articleの作成時間が指定時間内でなければ情報を返さないこと
    context "create_at < time" do
      it "does not return anything"
    end
  end

FactoryGirlの利用

テストデータを何度も書かなくて済むようにする

factoriesディレクトを作成。

/spec/factories/article.rb
FactoryGirl.define do
  factory :article do
    title: 'hoge'
    content: 'hogehoge'
  end
end

下記のように使用。

/spec/models/article_spec.rb
 #省略
describe Article do
  #titleとcontentが入っていれば有効であること
  it "is valid with title and content" do
    article = FactoryGirl.build(:article)
    expect(article).to be_valid
  end
end

FactoryGirlを楽に使う

Factory.build(:article) -> build(:article)と省略出来る。
下記を追加

/spec/spec_helper.rb
#ファクトリを簡単に呼び出せるように、FactoryGirlの構文をインクルードする
config.include FactoryGirl::Syntax::Methods

Fakerの利用

リアルなダミーデータを作成する

/spec/factories/article.rb
require 'faker'

FactoryGirl.define do
  factory :article do
    title: { Faker::Lorem.sentence }
    content: { Faker::Lorem.paragraph }
  end
end
title: "Et nisi et dolores et eveniet laborum et.",
content: "Consequuntur impedit minus blanditiis quos unde. Est voluptatem modi ratione culpa sunt iste. Dolores et tempore autem recusandae."

のようなダミーデータが入る。

その他、様々なダミーデータがあるので下記を参考に。
http://www.rubydoc.info/github/stympy/faker/master/frames

参考

Everyday Rails - RSpecによるRailsテスト入門
を参考にしております。詳細な説明・使い方を知りたい方はオススメです。

使えるRSpec入門・その1「RSpecの基本的な構文や便利な機能を理解する」
RSpecに関しましてはこちらも参考になります。その1~その4まであります。

123
139
2

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
123
139