rspec my best practice

Post on 04-Jul-2015

851 Views

Category:

Self Improvement

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

RSpec My Best Practice

TRANSCRIPT

MyBest

PracticeRSp

ec

13年2月3日日曜日

make

use ofGOOD LET

13年2月3日日曜日

examples

13年2月3日日曜日

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日日曜日

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日日曜日

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日日曜日

UsingDefining

forLET

Context★★

13年2月3日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

MECHANISMLazyEvaluation

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

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

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日日曜日

Clear Spec

Yes!!We Got

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

13年2月3日日曜日

Lazy EvaluationUsing

Readers...onBurden

13年2月3日日曜日

IT’STRADE-OFF

13年2月3日日曜日

To makeDRY SPECuse a fewLazy Evaluation

13年2月3日日曜日

Ifthere is manyLazy Evaluation,

13年2月3日日曜日

IT’S TIME TO

REFACTORING

and it’s a very very fun time :p

13年2月3日日曜日

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日日曜日

top related