ãã¤ã³ãã·ã¹ãã (1) -- æ°å¤ã®å¤åããã¹ããã
2013/09/19
ååã§ã¦ã¼ã¶ã¼èªè¨¼ã®ä»çµã¿ããã¡ããå®æããã®ã§ãä»åãããã°ããã®éãããã¤ã³ãã·ã¹ãã ãã®ä»æ§ãRSpecã§è¨è¿°ããå®è£ ãã¦ãããã¨ã«ãã¾ãã
ä»æ§ã®ãããã
ããã§è¨ãããã¤ã³ãã·ã¹ãã ãã¨ã¯ãããããªWebãµã¤ãã§è¡ããã¦ããä¾ã®ä»çµã¿ã§ããã¤ã¾ããã¦ã¼ã¶ã¼ã¯ãµã¤ãå ã§ã®è¡åã«å¿ãã¦ããã¤ã³ããã¨å¼ã°ããçä¼¼é貨ãåå¾ãã貯ãããã¤ã³ãã§ååããµã¼ãã¹ã¨äº¤æã§ããã¨ãããã®ã§ãããã®é£è¼ã§ã¯ãã¤ã³ã交æã®ã¨ããã¾ã§ä½ãè¾¼ãã¤ããã¯ãªãã®ã§ãããå°æ¥çã«ããããä»çµã¿ãå®è£ ãããã¨ã念é ã«ç½®ãã¦è¨è¨ãã¾ããã¾ãã顧客èªèº«ããã³Webãµã¤ãã®éå¶è ããã¤ã³ãå¢æ¸ã®è¨é²ï¼çºçæ¥æãé¡ãçç±ããªã©ï¼ãé²è¦§ã§ããä»çµã¿ãï¼æ¬é£è¼ã§ã¯ä½ããªãããã©ãï¼å°æ¥çã«å®è£ ããã¤ããã§ããããåãã¦ããã¾ãããã
ãã¦ããã¤ã³ãä»ä¸ã«é¢ããä»æ§ã¯ã¦ã¼ã¶ã¼èªè¨¼ã®ãã¹ã(1) -- Outside-Inã§æ¸ãã¾ããããããããã¦è¼ãã¦ããã¾ãï¼
- ã¦ã¼ã¶ã¼ããã°ã¤ã³ã«æåããã¨ãããã°ã¤ã³ãã¤ã³ããã¨ãã¦ã¦ã¼ã¶ã¼ã«1ãã¤ã³ããä¸ããããã
- ãã ããããã°ã¤ã³ãã¤ã³ããã¯ã¦ã¼ã¶ã¼ãã¨ã«1æ¥1åããä¸ããããªããæ¥ã®åºåãã¯æ¥æ¬æéåå5æã¨ããã
- ã¾ããåææ¥ã«ãã°ã¤ã³ããã¨ãé常ã®ããã°ã¤ã³ãã¤ã³ããã®ä»ã«ãåæãã°ã¤ã³ãã¼ãã¹ãã¨ãã¦2ãã¤ã³ããä¸ããããã
ä»åã¯1çªç®ã®ä»æ§ã®ãã¹ããæ¸ãã¦çµããã«ãã¾ãã
æ°å¤ã®å¤åã«é¢ãããã¹ãã®æ¸ãæ¹
1çªç®ã®ä»æ§ãè¨ãæããã¨ããã¦ã¼ã¶ã¼ããã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãããã¨ãããã¨ã§ãã仮㫠Customer#points
ã¡ã½ãããã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãï¼æ´æ°ï¼ã§è¿ãã¨ããã°ããã¹ãã³ã¼ãã¯æ¬¡ã®ããã«ãªãã¾ãã
describe Customer, '.authenticate' do let(:customer) { create(:customer, username: 'taro', password: 'correct_password') } # (çç¥) specify 'ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do expect { Customer.authenticate(customer.username, 'correct_password') }.to change { customer.points }.by(1) end end
å°ãè¤éãªã®ã§ãè¨å·ã使ã£ã¦ã¨ã°ã¶ã³ãã«ã®ä¸ãæ¸ãæããã¨æ¬¡ã®ããã«æ´çã§ãã¾ãã
expect { X }.to change { Y }.by(Z)
X
ãæ°å¤å¤åã®åå ã¨ãªãã³ã¼ãã§ããY
ã¯å¤åã調ã¹ããæ°å¤ãè¿ãã¡ã½ããã§ãããã㦠Z
ãæ°å¤ã®å¤åéã§ããX
ã«ã¯ãã¦ã¼ã¶ã¼ããã°ã¤ã³ã«æåããããæå³ããã³ã¼ãããY
ã«ã¯ Customer#points
ã¡ã½ããã®å¼ã³åºããããã㦠Z
ã«ã¯æ´æ°å¤ 1
ãæ¸ãå
¥ãã¾ãã
ã¾ãã¯ãã®ç¶æ ã§ãã¹ããå®è¡ãã¦ãã·ã³ã¿ãã¯ã¹ã¨ã©ã¼ãªã©ãèµ·ããªããã¨ã確èªãã¦ãã ããã
Failures: 1) Customer.authenticate ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã Failure/Error: }.to change { customer.points }.by(1) NoMethodError: undefined method `points' for #<Customer:0xba6b6a10> # ./spec/models/customer_spec.rb:111:in `block (3 levels) in <top (required)>' # ./spec/models/customer_spec.rb:109:in `block (2 levels) in <top (required)>'
ã¡ã½ããã®ä»®å®è£
ã¨ã°ã¶ã³ãã«ãæ£ããæ¸ãã¦ãããã¨ã示ããããCustomer#points
ã¡ã½ãããä»®å®è£
ãã¦ãã¹ããæ£ãã失æãããã¨ã確èªãã¾ãããã
describe Customer, '.authenticate' do let(:customer) { create(:customer, username: 'taro', password: 'correct_password') } # (çç¥) specify 'ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do customer.stub(:points).and_return(0) expect { Customer.authenticate(customer.username, 'correct_password') }.to change { customer.points }.by(1) end end
stub
ã¡ã½ããã«ã¤ãã¦ã¯ãã¦ã¼ã¶ã¼èªè¨¼ã®ãã¹ã(3)ã§èª¬æãã¾ãããand_return
ã§æå®ããå¤ï¼0
ï¼ã常ã«è¿ãã¡ã½ããã¨ã㦠points
ã¡ã½ãããä»®ã«ï¼ãã¹ãã®éã ãï¼å®è£
ãã¦ããã¾ãã
ã§ãçµæã¯æ¬¡ã®éãï¼
Failures: 1) Customer.authenticate ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã Failure/Error: expect { result should have been changed by 1, but was changed by 0 # ./spec/models/customer_spec.rb:110:in `block (2 levels) in <top (required)>'
pending
ã¡ã½ãã
Customer#points
ã¡ã½ããã¯ã©ããã風ã«å®è£
ãããããã§ããããããã¼ã¿ãã¼ã¹ã®ã¹ãã¼ããå¤æ´ããªãã¦ã¯ãªããªãã®ã§å°ãæéããããããã§ãããããªå ´åã¯ãpending
ã¡ã½ããã使ã£ã¦ããã³ãã£ã³ã°ãã«ãã¦ããã¾ãã
describe Customer, '.authenticate' do let(:customer) { create(:customer, username: 'taro', password: 'correct_password') } # (çç¥) specify 'ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do pending('Customer#pointsãæªå®è£ ') customer.stub(:points).and_return(0) expect { Customer.authenticate(customer.username, 'correct_password') }.to change { customer.points }.by(1) end end
RSpec ã® pending
ã¡ã½ããã«ã¯å¼æ°ã¨ãã¦ãã³ãã£ã³ã°ã«ããçç±ã説æããæååãæå®ãã¾ãããã®å¼æ°ã¯çç¥ã§ãã¾ãã
ãã¹ãã®å®è¡çµæã¯æ¬¡ã®ããã«ãªãã¾ãï¼
$ bin/rspec spec/models/customer_spec.rb *............................. Pending: Customer.authenticate ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã # Customer#pointsãæªå®è£ # ./spec/models/customer_spec.rb:108 Finished in 1.08 seconds 30 examples, 0 failures, 1 pending Randomized with seed 7827
ãã¹ãã®æåã示ãããã .
ã®é¨åããã¢ã¹ã¿ãªã¹ã¯ *
ã«å¤ãã£ã¦ãã¾ããã¾ããä¸ãã3è¡ç®ã«ã 1 pending
ã¨è¡¨ç¤ºããã¦ãã¾ãã
ãã¹ãã失æããã¾ã¾ã§ã¯æ°æã¡æªãã§ããããããªã Git ã Mercurial ã®ãªãã¸ããªã«ã³ããããã¦ãæ§ããªãã§ãããã
次åã¯
次åã¯ãCustomer#points
ã¡ã½ãããå®è£
ãã¾ããã§ã¯ãã¾ãã