- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(RSpec);
*目次 [#h9e8cd00]
#contents
*参考情報 [#n67a647c]
*関連ページ [#uc148ed0]
-[[Rails]]
-[[WebMock]]
-[[database_cleaner]]
-[[factory_girl]]
-[[Rubyist Magazine - スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編):http://jp.rubyist.net/magazine/?0021-Rspec]]
-[[RSpecの標準Matcher一覧表 - 本当は怖い情報科学:http://d.hatena.ne.jp/keisukefukuda/20080124/p1]]
-[[RSpecでテストコードを書いたまとめ - (゚∀゚)o彡 sasata299's blog:http://blog.livedoor.jp/sasata299/archives/51277861.html]]
-[[./概要]]
-[[./expect]]
-[[./Rails3へ導入]]
-[[./Rails4へ導入]]
-[[./モデルスペック]]
-[[./トラブルシューティング]]
-[[./Tips]]
-[[Rails3+Guard+Spork+RSpec]]
*RSpecとは何か [#g48e3d61]
-プログラムの振る舞いを記述するためのドメイン特化言語
#pre{{
describe Array, "when empty" do
before do
@empty_array = []
end
-[[Rails4+Guard+Spring+RSpec]]
-[[RSpec3]]
it "should be empty" do
@empty_array.should be_empty
end
*実行方法 [#g90dbbd6]
-全体実行
bundle exec rspec
-指定のファイルだけ実行 [#x0b03464]
bundle exec rspec spec/models/book_spec.rb
it "should size 0" do
@empty_array.size.should == 0
end
after do
@empty_array = nil
end
end
}}
*使用方法(簡単編) [#n3877647]
-rspecをインストール
-array_spec.rbを作る。
#pre{{
describe Array, "when empty" do
before do
@empty_array = []
end
it "should be empty" do
@empty_array.should be_empty
end
it "should size 0" do
@empty_array.size.should == 0
end
after do
@empty_array = nil
end
end
}}
-実行。-cオプションをつけると色を表示できる(rspec -c ...)
#pre{{
$ rspec array_spec.rb
rspec array_spec.rb
..
Finished in 0.00312 seconds
2 examples, 0 failures
}}
*使用方法(Rails4編) [#n54af1c0]
**インストール [#b527657d]
-Gemfileに追加
#pre{{
group :development, :test do
gem "rspec-rails", "~>2.14.0"
gem "factory_girl_rails", "~>4.2.1"
end
group :test do
gem "faker", "~>1.1.2"
gem "capybara", "~>2.1.0"
gem "database_cleaner", "~>1.0.1"
gem "launchy", "~>2.3.0"
gem "selenium-webdriver", "~>2.39.0"
end
}}
-bundle実行
bundle install --path vendor/bundle
-インストール
#pre{{
bundle exec rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
}}
-上のように、Gemfileでgroupを指定したときは、config/application.rbを編集する。これでモデルやコントローラーを作成したときに、*_spec.rbファイルを作る挙動を制御できる。
#pre{{
config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: false
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
}}
※注意事項)
-ちなみに以下のようにgroup指定して、rspec-railsを使う場合、普通にscaffoldするとspecが作られない。
#pre{{
group :development, :test do
gem "rspec-rails"
end
}}
-これはbundle execの環境がproductionであるため。group指定を以下のようにあらためるか(意味ないけど…)
#pre{{
group :production, :development, :test do
gem "rspec-rails"
end
}}
-RAILS_ENVを指定する。
RAILS_ENV=development bundle exec rails g
-でも間違えないように。config/application.rbをいじったほうが簡単。
*使用方法(Rails3編) [#t4d143c0]
-[[RSpecでRailsのテストをしてみるテスト。 | Ginpen.com:http://ginpen.com/2012/02/14/rspec-rails/]]
**インストール [#d3403589]
-Gemfileに追加
gem 'rspec-rails'
-bundle実行
bundle install --path=vendor/bundle
-RSpec用の共通ファイルを用意
#pre{{
bundle exec rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
}}
**テスト実行 [#had37ee3]
,bundle exec rake spec,spec以下の全てのspecを実行
,bundle exec rake spec,spec/modles以下の全てのspecを実行。
,bundle exec rake SPEC=spec/models/entry_spec.rb,個別のspecを実行
,bundle exec rspec spec/models/entry_spec.rb,個別のspecを実行rakeより速いかも。
**サンプル [#xfd6f6e2]
-[[Rails 3 でやってみた --『スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編)』 - 牌語備忘録 - pygo:http://d.hatena.ne.jp/CortYuming/20120526/p3]]
-ブログサンプルを作ってみる
***railsプロジェクトを生成 [#uf9d9337]
-普通に生成。
***インストール [#p25596a7]
-Gemfileに追加し、bundle実行
***モデルの生成 [#rda8095a]
-bundleで実行する
#pre{{
$ bundle exec rails generate model Blog name:string
invoke active_record
create db/migrate/20130419052115_create_blogs.rb
create app/models/blog.rb
invoke rspec
create spec/models/blog_spec.rb
}}
***マイグレーション実行 [#z94b4d6b]
-null不可に変更。
#pre{{
class CreateBlogs < ActiveRecord::Migration
def change
create_table :blogs do |t|
t.string :name, :null => false
t.timestamps
end
end
end
}}
-マイグレーションを実行する。
$ bundle exec rake db:migrate
***フィクスチャ作成 [#h186b8ed]
-spec/fixtures/blogs.ymlを作る
#pre{{
one:
id: 1
name: その1
two:
id: 2
name: その2
}}
***spec作成 [#n7c8f91a]
-spec/models.blog_spec.rb
#pre{{
# -*- coding: utf-8 -*-
require 'spec_helper'
describe Blog, "#name が設定されていない場合:" do
before(:each) do
@blog = Blog.new
end
it "バリデーションに失敗すること" do
@blog.should_not be_valid
end
end
}}
***spec実行 [#jbb0152d]
-rake spec SPEC=spec/models/blog_spec.rbを実行する 。この段階では失敗。
bundle exec rake pec SPEC=spec/models/blog_spec.rb
-次のようにvalidateすれば成功するようになる
#pre{{
class Blog < ActiveRecord::Base
attr_accessible :name
validates :name, presence:true
end
}}
*マッチャー一覧 [#m9de2191]
-xxx.should マッチャー / yyy.should_not マッチャーのように使用する(==もマッチャー)。
-[[RSpecの標準Matcher一覧表 - 本当は怖い情報科学:http://d.hatena.ne.jp/keisukefukuda/20080124/p1]]
*Tips [#q700f196]
**データファイルを読み込む [#u032113d]
-[[ruby on rails - relative File Path in RSpec - Stack Overflow:http://stackoverflow.com/questions/9345412/relative-file-path-in-rspec]]にあるように、__FILE__を使うしかない?
*トラブルシューティング [#ob7d9adf]
**spec実行時に「`require': cannot load such file -- spec_helper (LoadError)」 [#t6653edb]
-spec_helperを作ってないのかもしれない。以下のコマンドを実行してみる。
rails generate rspec:install