ãã¤ã³ãã·ã¹ãã (8) -- ãã¼ã¿ãã¼ã¹ãã©ã³ã¶ã¯ã·ã§ã³
2013/10/06
ããã¤ã³ãã·ã¹ãã (1) -- æ°å¤ã®å¤åããã¹ãããããã8åã«æ¸¡ã£ã¦ä½ã£ã¦ãããã¤ã³ãã·ã¹ãã ã¯ä»åã§ã²ã¨ã¾ãå®æã§ãã
åæãã°ã¤ã³ãã¼ãã¹
ãã¤ã³ãã·ã¹ãã ã§ã¾ã å®è£ ããã¦ããªãã®ã¯ããåæãã°ã¤ã³ãã¼ãã¹ãã®ä»çµã¿ã§ããä»æ§æ¸ã«ã¯ããæ¸ããã¦ãã¾ãï¼
ã¾ããåææ¥ã«ãã°ã¤ã³ããã¨ãé常ã®ããã°ã¤ã³ãã¤ã³ããã®ä»ã«ãåæãã°ã¤ã³ãã¼ãã¹ãã¨ãã¦2ãã¤ã³ããä¸ããããã
æ¥ã®åºåãã¯æ¥æ¬æéåå5æãªã®ã§ãåææ¥ã¨ã¯åææ¥ã®åå5æããæ¥ææ¥ã®åå5æã¾ã§ãã¨èãããã¨ã«ãã¾ãããããã ããçµç«¯ï¼æ¥ææ¥ã®åå5æã¡ããã©ï¼ã¯åææ¥ã«å«ã¾ãã¾ããã
ç§ãæ¸ãããã¹ãã³ã¼ãã¯æ¬¡ã®éãã§ãï¼
require 'spec_helper' describe RewardManager, '#grant_login_points' do let(:customer) { create(:customer) } let(:date_boundary) { Time.zone.local(2013, 1, 1, 5, 0, 0) } before { Time.zone = 'Tokyo' } specify 'åææ¥ã®åå5æç´åã«ãã°ã¤ã³ããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do Timecop.freeze(Time.zone.local(2013, 1, 5, 4, 59, 59)) expect { RewardManager.new(customer).grant_login_points }.to change { customer.points }.by(1) end specify 'åææ¥ã®åå5æã«ãã°ã¤ã³ããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã3å¢ãã' do Timecop.freeze(Time.zone.local(2013, 1, 5, 5, 0, 0)) expect { RewardManager.new(customer).grant_login_points }.to change { customer.points }.by(3) end specify 'æ¥ææ¥ã®åå5æç´åã«ãã°ã¤ã³ããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã3å¢ãã' do Timecop.freeze(Time.zone.local(2013, 1, 6, 4, 59, 59)) expect { RewardManager.new(customer).grant_login_points }.to change { customer.points }.by(3) end specify 'æ¥ææ¥ã®åå5æã«ãã°ã¤ã³ããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do Timecop.freeze(Time.zone.local(2013, 1, 6, 5, 0, 0)) expect { RewardManager.new(customer).grant_login_points }.to change { customer.points }.by(1) end specify 'æ¥ä»å¤æ´æå»ãã¾ããã§2åãã°ã¤ã³ããã¨ãRewardã2åä¸ãããã' do expect { Timecop.freeze(date_boundary.advance(seconds: -1)) RewardManager.new(customer).grant_login_points Timecop.freeze(date_boundary) RewardManager.new(customer).grant_login_points }.to change { customer.rewards.size }.by(2) end specify 'æ¥ä»å¤æ´æå»ãã¾ãããã«2åãã°ã¤ã³ãã¦ããRewardã1åããä¸ããããªã' do expect { Timecop.freeze(date_boundary) RewardManager.new(customer).grant_login_points Timecop.freeze(date_boundary.advance(hours: 24, seconds: -1)) RewardManager.new(customer).grant_login_points }.to change { customer.rewards.size }.by(1) end end
æ¥ä»å¤æ´æå»ã§ããåå5æã®ç´åã¨åå5æã¡ããã©ã«ã¤ãã¦ãä¸å¯§ã«ãã¹ããæ¸ãã¾ãããã¾ãã以åæ¸ããã2åãã°ã¤ã³ãã«é¢ãããã¹ãã¯ãä¿æãã¤ã³ãã®å¤åã§ã¯ãªã Reward ãªãã¸ã§ã¯ãã®åæ°ã®å¤åã«å¤ãã¾ããã
å®è£ ã¯ãã£ããªãã»ã©ç°¡åã§ãï¼
def grant_login_points Time.zone = 'Tokyo' now = Time.current if now.hour < 5 time0 = now.yesterday.midnight.advance(hours: 5) time1 = now.midnight.advance(hours: 5) else time0 = now.midnight.advance(hours: 5) time1 = now.tomorrow.midnight.advance(hours: 5) end unless customer.rewards.where(created_at: time0...time1).exists? customer.rewards.create(points: 1) customer.rewards.create(points: 2) if time0.wday == 6 end end
ä¸ãã3è¡ç®ã追å ããã¦ãã¾ããæ¥ä»ã¾ãã¯æå»ãªãã¸ã§ã¯ãã® wday
ã¡ã½ããã¯ææ¥ã表ãæ´æ°å¤ãè¿ãã¾ããæ¥ææ¥ã 0 ã§ãåææ¥ã¯ 6 ã§ããå¤æ° time0
ã«ã¯ããã°ã¤ã³ãã¤ã³ãã®è¨ç®ã«ä½¿ç¨ããæ¥ä»ï¼åå5æããåãªãåæ¥ï¼ã®éå§æå»ï¼åå5æï¼ãªã®ã§ãtime0.wday == 6
ãªãã°ãåæãã°ã¤ã³ãã¼ãã¹ããä»ä¸ãã¦ããã¨ãããã¨ã«ãªãã¾ãã
ãã¼ã¿ãã¼ã¹ãã©ã³ã¶ã¯ã·ã§ã³
å®ã¯ãå ã»ã©ã®å®è£ ã«ã¯ä¸åãããã¾ããããã¯ããã¼ã¿ãã¼ã¹ãã©ã³ã¶ã¯ã·ã§ã³ã®ãã¨ãèæ ®ããã¦ããªããã¨ãããã¨ã§ããåææ¥ã«ãã°ã¤ã³ãã顧客ã«å¯¾ãã¦ãé常ã®ãã°ã¤ã³ãã¤ã³ã 1 ãä»ä¸ããç´å¾ã«ããã¼ã¿ãã¼ã¹ç®¡çã·ã¹ãã ãä½ããã®çç±ã§å©ç¨ã§ããªããªãã¨ããã®é¡§å®¢ã«ã¯ãåæãã°ã¤ã³ãã¼ãã¹ããä¸ãããã¾ããããã°ã¤ã³ã失æãããã¨ã«ãªãã¾ããããã¦ããã®é¡§å®¢ãæ¹ãã¦ãã°ã¤ã³ãç´ããã¨ãã¦ãããã§ã«ãã°ã¤ã³ãã¤ã³ã 1 ãä»ä¸ããè¨é²ãæ®ã£ã¦ããã®ã§ããã¯ããåæãã°ã¤ã³ãã¼ãã¹ããä»ä¸ãããªãã¨ãããã¨ã«ãªãã®ã§ãã
ãã®äºæ
ãé¿ããã«ã¯ã次ã®ããã«ãã¤ã³ãä»ä¸å¦çå
¨ä½ã ActiveRecord::Base.transaction do ... end
ã§å²ã¿ã¾ãï¼
def grant_login_points Time.zone = 'Tokyo' now = Time.current if now.hour < 5 time0 = now.yesterday.midnight.advance(hours: 5) time1 = now.midnight.advance(hours: 5) else time0 = now.midnight.advance(hours: 5) time1 = now.tomorrow.midnight.advance(hours: 5) end ActiveRecord::Base.transaction do unless customer.rewards.where(created_at: time0...time1).exists? customer.rewards.create(points: 1) customer.rewards.create(points: 2) if time0.wday == 6 end end end
ãã¦ããã®ä¿®æ£ãåãã¦ãã¹ãã®æ¹ã¯æ¸ãæããªãã¦ãããã®ã§ããããããã©ã³ã¶ã¯ã·ã§ã³å¦çãè¡ããã¦ãããã¨ã確ãããªãã¦ãããã®ã§ããããã
çµè«ããè¨ãã°ããã®ã±ã¼ã¹ã§ã¯ãã¹ãã追å ããå¿ è¦ã¯ãªããã¨ç§ã¯èãã¾ãã
ã¨ããã®ã¯ãããã¡ã½ããã®ä¸ã§ãã¼ã¿ãã¼ã¹ãã©ã³ã¶ã¯ã·ã§ã³ã使ç¨ããã¦ãããã©ããã確èªããã®ã¯ãæå¤ã«é£ããããã§ããRewardManager#grant_login_points
ã¡ã½ããã®ä¸ã« ActiveRecord::Base.transaction do ... end
ããªããã°å¤±æããããã°æåãããããªã¨ã°ã¶ã³ãã«ãæ¸ããªããã¨ãããã¾ããããããªãé¢åã§ããå´åã«è¦åããªãããã«ç§ã«ã¯æãã¾ãã
ãã®ãã¼ããããã«è¿½æ±ãããæ¹ã¯ã以ä¸ã®ãã¼ã¸ãåç §ãã¦ãã ããï¼
- http://stackoverflow.com/questions/10511236/how-to-test-that-a-certain-function-uses-a-transaction-in-rails-and-rspec-2
- http://strix01.blogspot.jp/2012/07/rails-rspecdb.html
次åã¯
次åã®æ²è¼ã¯ãå°ãå ã®ãã¨ã«ãªãããã§ããã¾ã å 容ã¯æ±ºãã¦ãã¾ãããå°ã ãå¾ ã¡ãã ãããã§ã¯ãã¾ãã