Everyday Rails - 10日目
10.その他のテスト Timecop
Gemfileに testグループに gem 'timecop' を追加。
例を通すためにcontrollerを作る。indexだけあればよい。
./bin/raile g controller PayTaxes index
- ./app/controllers/pay_taxes_controller.rb
class PayTaxesController < ApplicationController def index now = Time.now if now >= Time.parse("1/1") && now <= Time.parse("4/15").end_of_day @message = "There's still time to file, but hurry!" else @message = "Sorry, you're too late!" end end end
- ./app/views/pay_taxes/index.html.haml
%h1 PayTaxes#index %p= @message
- ./spec/features/pay_taxes_spec.rb
require 'rails_helper' feature 'PayTaxes', type: :feature, js: true do scenario "does'nt allow taxpayers to file April 15" do Timecop.travel Time.parse("4/16") visit pay_taxes_path expect(page).to have_content "Sorry, you're too late!" page.save_screenshot(sc_file('pay_taxes_1.png')) Timecop.return end scenario "gives taxpayers up until the 15th to file" do Timecop.travel Time.parse("4/15 10:00") visit pay_taxes_path expect(page).to have_content "There's still time to file, but hurry!" page.save_screenshot(sc_file('pay_taxes_2.png')) Timecop.return end end
- ./spec/models/user_spec.rb
require 'rails_helper' describe User, type: :model do describe "create" do it "created_at に 作成日時がはいること" do Timecop.freeze expect(create(:user).created_at).to eq Time.now Timecop.return end end end
今日の感想
Timecopは使える。今回も例とちょっと変えている。
Nitrous.IOはターミナルでは日本語つかうと文字位置とカーソル位置がずれるので、その場合はエディタで。
で思ったけど、Desktopという選択肢もあるか。
Nitrous
でもそこまでするならローカルでやるかも。