[RSpec] Time.now をスタブ化
- 2013 3/26
Ruby on Rails + RSpec で Time.now をスタブ化するコードをメモ。
time_now = Time.local(2013, 12, 31, 0, 0, 0) Time.stub!(:now).and_return(time_now) |
Ruby on Rails + RSpec で Time.now をスタブ化するコードをメモ。
time_now = Time.local(2013, 12, 31, 0, 0, 0) Time.stub!(:now).and_return(time_now) |
Ruby on Rails + RSpec でテストを走らす前に SeedFu.seed で初期データを流し込んで運用しています。
シードファイルの読み込み先を変更したい場合は SeedFu.fixture_paths に配列で path を渡してあげればOKです。
# spec/spec_helper.rb RSpec.configure do |config| # Seed_fu config.before(:suite) do SeedFu.fixture_paths = [ "#{Rails.root}/db/fixtures", "#{Rails.root}/db/fixtures_initialization" ] SeedFu.seed end end |
Ruby on Rails + RSpec でのファイルアップロードするためのメソッドをメモ。
fixture_file_upload |
Rack::Test::UploadedFile.new |
ActionController::TestUploadedFile.new |
ActionDispatch::Http::UploadedFile.new |
[参考]:ruby – How do I test a file upload in rails? – Stack Overflow
RSpec で、定数を stub(スタブ)化するには stub_const メソッドを使います。
(例)定数 FOO をスタブ化
FOO = 7 describe "stubbing FOO" do it "can stub FOO with a different value" do stub_const("FOO", 5) FOO.should eq(5) end it "restores the stubbed constant when the example completes" do FOO.should eq(7) end end |
(例)Class や Module の定数をスタブ化
module MyGem class SomeClass FOO = 7 end end module MyGem describe SomeClass do it "stubs the nested constant when it is fully qualified" do stub_const("MyGem::SomeClass::FOO", 5) SomeClass::FOO.should eq(5) end end end |
[参考]:Stub Defined Constant – Stubbing constants – RSpec Mocks – RSpec – Relish
RSpec で、コントローラーのテストで Ajax リクエストを送るには xhr を使えば良いらしいです。
■ Ajax GET
xhr :get, :index |
■ Ajax POST
xhr :post, :create, id: 1 |
下記のような感じで使えます。カンタンですね!
describe "Ajax GET 'index'" do it "returns http success" do xhr :get, :index response.should be_success end end |
・[参考]:ruby on rails – How do you test an AJAX request with RSpec/RoR? – Stack Overflow
Ruby on Rails + RSpec で privateメソッドをテストする方法をメモ。
下記のように、Userモデルに private_method というprivateメソッドを作成した場合、
■ app/models/user.rb
class User < ActiveRecord::Base def public_method "public" end private def private_method "private" end end |
Rspec でのモデルのテストは下記のように、user.send(:private_method) で呼び出して行う。
■ spec/models/user_spec.rb
require 'spec_helper' describe User do it 'public' do user = User.new user.public_method.should == "public" end it 'private' do user = User.new user.send(:private_method).should == "private" end end |
【参考】
Ruby on Rails + RSpec でテストコードを書くときの Devise の使い方いろいろメモ。
主にここを読む。
・How To: Controllers and Views tests with Rails 3 (and rspec) · plataformatec/devise Wiki
■ current_user にスタブを使う
Controllerのテスト
before do controller.stub!(:current_user).and_return(FactoryGirl.create(:user)) end |
他にも。
Ruby のテストフレームワーク「RSpec」を入門する上で参考になった記事をまとめてみました。
・Rubyist Magazine – スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編)
・Rubyist Magazine – スはスペックのス 【第 2 回】 RSpec on Rails (コントローラとビュー編)
・Rubyist Magazine – 改めて学ぶ RSpec
・RSpecによるユニットテストの書き方 – tech.recompile.net
・Twiwt:Blog / jugyo : spork でサクサク RSpec on Rails3
・RSpecでRailsのテストをしてみるテスト。 | Ginpen.com
・RSpecでテストコードを書いたまとめ – (゚∀゚)o彡 sasata299’s blog
・私はRSpecでテストをこんな感じで書いてる – アジャイルSEを目指すブログ
・ビューをRSpecでテストする際の覚え書き – MothProgのうしろがわ
・aRailsDemo | #37 RSpec – Views Part 1
・ASCIIcasts – “Episode 257 – request specとCapybara”
contain, have_selector, have_xpath などの利用例
・最近話題のRSpecのプレゼンの中で知らなかったこと – takihiroの日記
Devise でのテスト
・How To: Controllers and Views tests with Rails 3 (and rspec) · plataformatec/devise Wiki
とりあえず、以上。
RSpec の stub(スタブ)と mock(モック)の違いについて調べたので、参考になったサイトをメモ。
とりあえず、以上です。