Your SlideShare is downloading. ×
0
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Rspec
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rspec

1,167

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,167
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. RSpec documentationhttp://relishapp.com/rspec
  • 2. components RSpec CoreRSpec Expectations RSpec Mocks RSpec Rails
  • 3. run specsrake specrake spec:modelsrspec spec/models/order_spec.rbrspec spec/models/order_spec.rb:19
  • 4. "be" matchersdescribe 0 do it { subject.zero?.should == true } # vs. it { should be_zero } # 0.zero? == trueend
  • 5. have(n).items matchercollection.should have(x).itemscollection.should have_at_least(x).itemscollection.should have_at_most(x).itemsit { should have(5).line_items }
  • 6. subject
  • 7. implicit subjectdescribe Comment do subject { Comment.new }end
  • 8. explicit subjectdescribe Order do subject { Factory.build(:order) }end
  • 9. implicit receiverit { subject.should be_valid }# vs.it { should be_valid }
  • 10. itsits(:status_name) { should == :draft }its(line_items.count) { should == 5 }
  • 11. letbefore do @order = Factory.create(:shipped_order)end# vs.let(:order) do Factory.create(:shipped_order)end
  • 12. context vs. describe alias :context :describe describe is for methods context is for contexts
  • 13. class & instance methodsdescribe #title do # instance method ...enddescribe .title do # class method ...end
  • 14. contextcontext when quantity is negative do before { subject.quantity = -1 } ...end
  • 15. model validationsit { should be_invalid }its(:errors) { should be_present }it { should have(1).errors_on(:quantity) }
  • 16. shoulda-matchersit should validate email and quantity do subject.email = test@example.com subject.quantity = -1 subject.valid? should have(:no).errors_on(:email) should have_at_least(1).error_on(:quantity)end# vs.it { should allow_value(test@example.com).for(:email) }it { should_not allow_value(-1).for(:quantity) }
  • 17. mocksfake objects and methods dont touch database faster specs
  • 18. rails model mocksmock_model is a test double that acts like an ActiveModel stub_model is a instance of a real ActiveModel with some methods stubbed
  • 19. using mockslet(:email_template) do stub_model(EmailTemplate, :name => test_email, ...)endbefore do ... EmailTemplate.stub(:find_by_name). with(test_email). and_return(email_template)end
  • 20. message expectationdef should_deliver_mail(method) Mailer.should_receive(method). with(subject). and_return(double(:deliver => nil))end
  • 21. questions?

×