Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rspec

1,502 views

Published on

  • Be the first to comment

Rspec

  1. 1. RSpec documentationhttp://relishapp.com/rspec
  2. 2. components RSpec CoreRSpec Expectations RSpec Mocks RSpec Rails
  3. 3. run specsrake specrake spec:modelsrspec spec/models/order_spec.rbrspec spec/models/order_spec.rb:19
  4. 4. "be" matchersdescribe 0 do it { subject.zero?.should == true } # vs. it { should be_zero } # 0.zero? == trueend
  5. 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. 6. subject
  7. 7. implicit subjectdescribe Comment do subject { Comment.new }end
  8. 8. explicit subjectdescribe Order do subject { Factory.build(:order) }end
  9. 9. implicit receiverit { subject.should be_valid }# vs.it { should be_valid }
  10. 10. itsits(:status_name) { should == :draft }its(line_items.count) { should == 5 }
  11. 11. letbefore do @order = Factory.create(:shipped_order)end# vs.let(:order) do Factory.create(:shipped_order)end
  12. 12. context vs. describe alias :context :describe describe is for methods context is for contexts
  13. 13. class & instance methodsdescribe #title do # instance method ...enddescribe .title do # class method ...end
  14. 14. contextcontext when quantity is negative do before { subject.quantity = -1 } ...end
  15. 15. model validationsit { should be_invalid }its(:errors) { should be_present }it { should have(1).errors_on(:quantity) }
  16. 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. 17. mocksfake objects and methods dont touch database faster specs
  18. 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. 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. 20. message expectationdef should_deliver_mail(method) Mailer.should_receive(method). with(subject). and_return(double(:deliver => nil))end
  21. 21. questions?

×