LoginSignup
22
22

More than 5 years have passed since last update.

RSpecをRailsにインストールする

Last updated at Posted at 2014-11-25

Gemfileへの追加

Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails', '~> 3.0'
end

gemのインストール

$ bundle install

RSPECを初期化

$ rails g rspec:install

DBを初期化

MySQLを利用している場合は、CREATE DATABASEする

$ rake db:migrate RAILS_ENV=test

※ RSpecの実行環境は「test」なので注意してください。

Warningの削除

そのまま実行するとWarningが大量に出てしまいます。内容を確認したところ、sassなどライブラリ周りで出ているようです。Warningが出っ放しなのはちょっと嫌なのですが、ここは致し方なくWarningが出力されないようにしましょう

.rspec
--color
#--warnings
--require spec_helper

シャープでコメントアウトします。

RSpecを書こう

spec/example/my_example_spec.rb
require 'rails_helper'

RSpec.describe ApplicationHelper, :type => 'helper' do
  describe "example spec" do

    it "should aaaaaa" do
      p "test"
    end
  end
end

※RSpecの書き方はいくつか種類があるようですが、こちらが一番正当なのでしょうか?

参考: http://www.rubydoc.info/gems/rspec-rails/frames
※実施できるテストの種類は、Model Spec, Request Spec, Helper Specなど様々あります。

RSpecの実行

$ bundle exec rspec spec/example/

spec/example以下のすべてのテストが実行されます。

22
22
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
22
22