Top Banner
My Best Practice RSpec 1323日日曜日
32

RSpec My Best Practice

Jul 04, 2015

Download

Self Improvement

Koichi Tanaka

RSpec My Best Practice
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RSpec My Best Practice

MyBest

PracticeRSp

ec

13年2月3日日曜日

Page 2: RSpec My Best Practice

make

use ofGOOD LET

13年2月3日日曜日

Page 3: RSpec My Best Practice

examples

13年2月3日日曜日

Page 4: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

context "when user is administrator" do let(:user) do FactoryGirl.create :user, admin: true end

it "returns true" do expect(subject).to be_true 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 end

13年2月3日日曜日

Page 5: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

context "when user is administrator" do let(:user) do FactoryGirl.create :user, admin: true end

it "returns true" do expect(subject).to be_true 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 end

13年2月3日日曜日

Page 6: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

context "when user is administrator" do let(:user) do FactoryGirl.create :user, admin: true end

it "returns true" do expect(subject).to be_true 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 end

NOT DRY13年2月3日日曜日

Page 7: RSpec My Best Practice

UsingDefining

forLET

Context★★

13年2月3日日曜日

Page 8: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

context "when user is administrator" do let(:user) do FactoryGirl.create :user, admin: true end

it "returns true" do expect(subject).to be_true 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 end

13年2月3日日曜日

Page 9: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

13年2月3日日曜日

Page 10: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

Write ONLY changing parameters under ‘context’.By doing so, the spec is very clear and meaningful.

13年2月3日日曜日

Page 11: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

HOW TO

13年2月3日日曜日

Page 12: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

let(:user) { FactoryGirl.create :user, admin: admin }

★ Prepare resources using ‘let’, ‘let!’

and ‘before’ under describe.13年2月3日日曜日

Page 13: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

let(:user) { FactoryGirl.create :user, admin: admin }

★ Parameters changes in context is defined by ‘let’

under context, and use ONLY let at there.

13年2月3日日曜日

Page 14: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

context "when user is administrator" do let(:admin) { true }

★ Parameters changes in ‘context’ is defined by

‘let’ under context, and use ONLY ‘let’ at there.

context "when user is not administrator" do let(:admin) { false }

13年2月3日日曜日

Page 15: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

MECHANISM

13年2月3日日曜日

Page 16: RSpec My Best Practice

MECHANISMLazyEvaluation

http://www.fanpop.com/clubs/ho-kago-tea-time/images/29622703/title/dont-lazy-wallpaper13年2月3日日曜日

Page 17: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

13年2月3日日曜日

Page 18: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

13年2月3日日曜日

Page 19: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

★ At first, RSpec evaluates this line.

13年2月3日日曜日

Page 20: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

★ Then ‘subject’ is evaluated.

13年2月3日日曜日

Page 21: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

★ And ‘user’ defined by ‘let’

is evaluated.13年2月3日日曜日

Page 22: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

★ And ‘admin’ defined by ‘let’

is evaluated.13年2月3日日曜日

Page 23: RSpec My Best Practice

examples

subject { user.admin? }

let(:user) do FactoryGirl.create :user, admin: adminend

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true endend

FactoryGirl.create(:user, admin: true).admin?

★ Finally, ‘subject’ is evaluated to

this.13年2月3日日曜日

Page 24: RSpec My Best Practice

examples

describe User do describe '#admin?' do subject { user.admin? }

let(:user) { FactoryGirl.create :user, admin: admin }

context "when user is administrator" do let(:admin) { true }

it "returns true" do expect(subject).to be_true end end

context "when user is not administrator" do let(:admin) { false }

it "returns false" do expect(subject).to be_false end end endend

13年2月3日日曜日

Page 25: RSpec My Best Practice

Clear Spec

Yes!!We Got

13年2月3日日曜日

Page 26: RSpec My Best Practice

!!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!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION!!CATION

13年2月3日日曜日

Page 27: RSpec My Best Practice

Lazy EvaluationUsing

Readers...onBurden

13年2月3日日曜日

Page 28: RSpec My Best Practice

IT’STRADE-OFF

13年2月3日日曜日

Page 29: RSpec My Best Practice

To makeDRY SPECuse a fewLazy Evaluation

13年2月3日日曜日

Page 30: RSpec My Best Practice

Ifthere is manyLazy Evaluation,

13年2月3日日曜日

Page 31: RSpec My Best Practice

IT’S TIME TO

REFACTORING

and it’s a very very fun time :p

13年2月3日日曜日

Page 32: RSpec My Best Practice

blogged athttp://blog.tanaka51.jp/blog/2012/12/14/rspec-best-practice/

referenced byhttps://speakerdeck.com/holman

http://kotas.hatenablog.jp/entry/2012/11/22/000046

13年2月3日日曜日