RSpec My Best Practice
Upcoming SlideShare
Loading in...5
×
 

RSpec My Best Practice

on

  • 814 views

RSpec My Best Practice

RSpec My Best Practice

Statistics

Views

Total Views
814
Views on SlideShare
814
Embed Views
0

Actions

Likes
5
Downloads
12
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    RSpec My Best Practice RSpec My Best Practice Presentation Transcript

    • RS pMy e c Best Practice13年2月3日日曜日
    • make GOOD LET use of13年2月3日日曜日
    • pl e s e x a m13年2月3日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • Using LET for ★ Defining ★ n t e x t ★ C o13年2月3日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • MEC Lazy ISM HAN Evaluationhttp://www.fanpop.com/clubs/ho-kago-tea-time/images/29622703/title/dont-lazy-wallpaper13年2月3日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • 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日日曜日
    • Yes!! We Got Clear Spec13年2月3日日曜日
    • 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日日曜日
    • Using B u rd n Lazy Evaluation e on Readers...13年2月3日日曜日
    • IT’S TRADE-OFF13年2月3日日曜日
    • To make DRY SPEC use a few Lazy Evaluation13年2月3日日曜日
    • If there is many Lazy Evaluation,13年2月3日日曜日
    • IT’S TIME TO IN G T O R FA C R E and it’s a very very fun time :p13年2月3日日曜日
    • 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日日曜日