RSpec My Best Practice

1,247 views

Published on

RSpec My Best Practice

Published in: Self Improvement
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,247
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
17
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

RSpec My Best Practice

  1. 1. RS pMy e c Best Practice13年2月3日日曜日
  2. 2. make GOOD LET use of13年2月3日日曜日
  3. 3. pl e s e x a m13年2月3日日曜日
  4. 4. describe User do describe #admin? do s subject { user.admin? } l e context "when user is administrator" do p let(:user) do FactoryGirl.create :user, admin: true m end a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:user) do FactoryGirl.create :user, admin: false end it "returns false" do expect(subject).to be_false end end end13年2月3日日曜日
  5. 5. describe User do describe #admin? do s subject { user.admin? } l e context "when user is administrator" do p let(:user) do FactoryGirl.create :user, admin: true m end a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:user) do FactoryGirl.create :user, admin: false end it "returns false" do expect(subject).to be_false end end end13年2月3日日曜日
  6. 6. describe User do describe #admin? do s subject { user.admin? } l e context "when user is administrator" doNOTmDRY p let(:user) do FactoryGirl.create :user, admin: true end a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:user) do FactoryGirl.create :user, admin: false end it "returns false" do expect(subject).to be_false end end end13年2月3日日曜日
  7. 7. Using LET for ★ Defining ★ n t e x t ★ C o13年2月3日日曜日
  8. 8. describe User do describe #admin? do s subject { user.admin? } l e context "when user is administrator" do p let(:user) do FactoryGirl.create :user, admin: true m end a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:user) do FactoryGirl.create :user, admin: false end it "returns false" do expect(subject).to be_false end end end13年2月3日日曜日
  9. 9. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end end end end13年2月3日日曜日
  10. 10. describe User do describe #admin? do s subject { user.admin? } e context "when user is administrator" do l let(:user) { FactoryGirl.create :user, admin: admin } let(:admin) { true } p context "when user is administrator" do m let(:admin) { true } it "returnstrue" do do true" a it "returns expect(subject).to be_true x expect(subject).to be_true e end end end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end Write ONLY changing parameters under ‘context’. end end By doing so, the spec is very clear and meaningful. end13年2月3日日曜日
  11. 11. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } HOW TO p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end end end end13年2月3日日曜日
  12. 12. describe User do describe #admin? do s subject { user.admin? } l e let(:user) let(:user) { FactoryGirl.create :user, admin: }admin } { FactoryGirl.create :user, admin: admin p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do★ Prepare resources using ‘let’, ‘let!’ expect(subject).to be_false end end end and ‘before’ under describe. end13年2月3日日曜日
  13. 13. describe User do describe #admin? do s subject { user.admin? } l e let(:user) let(:user) { FactoryGirl.create :user, admin: }admin } { FactoryGirl.create :user, admin: admin p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false★ Parameters changes in context is defined by ‘let’ end end end under context, and use ONLY let at there. end13年2月3日日曜日
  14. 14. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } context "when user is administrator" do p let(:admin) "when user is}administrator" do context { true } m let(:admin) { true a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false is not administrator" do context "when user } let(:admin) { false } it "returns false" do expect(subject).to be_false★ Parameters changes in ‘context’ is defined by end end end ‘let’ under context, and use ONLY ‘let’ at there. end13年2月3日日曜日
  15. 15. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } p MECHANISM context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end end end end13年2月3日日曜日
  16. 16. MEC Lazy ISM HAN Evaluationhttp://www.fanpop.com/clubs/ho-kago-tea-time/images/29622703/title/dont-lazy-wallpaper13年2月3日日曜日
  17. 17. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end end end end13年2月3日日曜日
  18. 18. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end13年2月3日日曜日
  19. 19. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end ★ At first, RSpec evaluates this line.13年2月3日日曜日
  20. 20. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end ★ Then ‘subject’ is evaluated.13年2月3日日曜日
  21. 21. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end ★ And ‘user’ defined by ‘let’ is evaluated.13年2月3日日曜日
  22. 22. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end ★ And ‘admin’ defined by ‘let’ is evaluated.13年2月3日日曜日
  23. 23. subject { user.admin? } s let(:user) do l e FactoryGirl.create :user, admin: admin p end FactoryGirl.create(:user, admin: true).admin? m context "when user is administrator" do a let(:admin) { true } e x it "returns true" do expect(subject).to be_true end end ★ Finally, ‘subject’ is evaluated to this.13年2月3日日曜日
  24. 24. describe User do describe #admin? do s subject { user.admin? } l e let(:user) { FactoryGirl.create :user, admin: admin } p context "when user is administrator" do m let(:admin) { true } a it "returns true" do x expect(subject).to be_true e end end context "when user is not administrator" do let(:admin) { false } it "returns false" do expect(subject).to be_false end end end end13年2月3日日曜日
  25. 25. Yes!! We Got Clear Spec13年2月3日日曜日
  26. 26. CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!13年2月3日日曜日
  27. 27. Using B u rd n Lazy Evaluation e on Readers...13年2月3日日曜日
  28. 28. IT’S TRADE-OFF13年2月3日日曜日
  29. 29. To make DRY SPEC use a few Lazy Evaluation13年2月3日日曜日
  30. 30. If there is many Lazy Evaluation,13年2月3日日曜日
  31. 31. IT’S TIME TO IN G T O R FA C R E and it’s a very very fun time :p13年2月3日日曜日
  32. 32. blogged at http://blog.tanaka51.jp/blog/2012/12/14/rspec-best-practice/ referenced by https://speakerdeck.com/holman http://kotas.hatenablog.jp/entry/2012/11/22/00004613年2月3日日曜日

×