• 1. Rspec Best PracticesRuby Social Club Milano 24 February 2011 Andrea Reginato twitter/andreareginato
  • 2. Ill not teach RSpecYou have good books and web sites The RSpec BookRails Test Prescriptions twitter/andreareginato
  • 3. Ill tell you how I use RSpecWhat Ive learned reading books some articles on the webbut most important from my everyday job experiencetwitter/andreareginato
  • 4. Just a little introduction RSpec is a library that focus ontesting the behavior of youreveryday projecttwitter/andreareginato
  • 5. Why people do not use it Probably the testing practice is themost diffcult forma mentis adeveloper have to learn twitter/andreareginato
  • 6. It will save our life, maybeIn the long term, you will not checkyour web page everytime you make a new feature, but you willcheck if your test passedtwitter/andreareginato
  • 7. Short descriptions# wrongit "should have 422 status code if anunexpected params will be added" doresponse.should respond_with 422end# correctcontext "when not valid"it { should respond_with 422 }
  • 8. (over)use contextsand use with/when as keys# wrongit "should have 200 status code iflogged in" doresponse.should respond_with 200end# correctcontext "when logged out" doit { should respond_with 401 }end
  • 9. Describe methods# wrongdescribe "the authenticate method for User logged in" ...describe "if the user is an admin" do# correctdescribe ".authenticate" dodescribe "#admin?"
  • 10. Single expectation test# wrongit "should create a resource" doresponse.should respond_with_content_type(:json)response.should assign_to(:resource)end# correctit { should respond_with_content_type(:json) }it { should assign_to(:resource) }
  • 11. Test the edge cases# sample actiondef [email protected] = Resource.where(:id => params[:id])if @[email protected] 204elserender :template => "shared/404", :status => 404,endendWhat would you test ?
  • 12. Test the edge case# correctdescribe "#destroy" docontext "when resource is found" doit "should render_with 204"it "should assign @resource"context "when resource is not found" doit "should render with 404"it "should not assign @resource"endendIs this enough ?
  • 13. Use the subject# wrongit { assigns("message").should match /The resource name is Genoveffa/ }it { assigns("message").should match /it was born in Billyville/ }it { assigns("message").creator.should match /Claudiano/ }# correctsubject { assigns("message") }it { should match /The resource name is Genoveffa/ }it { should match /it was born in Billyville/ }its(:creator) { should match /Claudiano/ }
  • 14. Mock or not to mock# wrong (a really personal point of view)1) Its wrong when you never mock and recreate by codea “medium complex” situation and its time consuming2) Its wrong when you mock everything. You have a lightsystem and the big advantage of independence, but you loosepart of the control on your application.# correct# simulate authenticated usercontroller.stub!(:authenticate).and_return(true)# simulate current usercontroller.stub(:current_user).and_return(current_user)# simulate not found recordResource.stub(:where).with(created_from: id) .and_return(false)
  • 15. Create data when needed# wrongYou shouldnt use fixtures* loose control on our data* load everything all the time* difficult to find problems# correctdescribe "User"describe ".top" dobefore { 3.times { Factory(:user) } }it { User.top(2).should have(2).item }endend
  • 16. Shared examples to DRY# wrongcontext "when own resources" doit "should have it" doresource = Factory("user")do_get format: jsonassigns(users).should include(resource)endendcontext "when does not own resource" doit "should not have it" donot_owned_resource = Factory("unknown")do_get format: jsonassigns(users).should_not include(not_owned_resource)endend
  • 17. Shared examples to DRY# correct (but why?)shared_examples for "a secure resource" docontext "when own the resource" doit "should have it" doresource = Factory("user")do_get format: jsonassigns(users).should include(resource)endendcontext "when does not own resource" doit "should not have it" donot_owned_resource = Factory("unknown")do_get format: jsonassigns(users).should_not include(not_owned_resource)endendend
  • 18. Shared examples to DRY# correct (apply DRY on tests)describe "#show" doit_should_behave_like "a secure resource"it_should_behave_like "a findable resource"it_should_behave_like "a secure resource"context "when not logged in" doit_should_handle "a not authenticated request"endend
  • 19. More?# wrongDo not use RSpec# correctStart using RSpec 2 into your next project
  • 20. Thanks# more infoThere is a public google doc that Ive started with some friends,and whenever you want you can write me# [email protected]/andreareginato
    Please download to view
  • All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
    ...

    RSpec 2 Best practices

    by andrea-reginato

    on

    Report

    Category:

    Technology

    Download: 0

    Comment: 0

    506,481

    views

    Comments

    Description

    A list of some best practices I've been learning during my daily job.
    Download RSpec 2 Best practices

    Transcript

    • 1. Rspec Best PracticesRuby Social Club Milano 24 February 2011 Andrea Reginato twitter/andreareginato
  • 2. Ill not teach RSpecYou have good books and web sites The RSpec BookRails Test Prescriptions twitter/andreareginato
  • 3. Ill tell you how I use RSpecWhat Ive learned reading books some articles on the webbut most important from my everyday job experiencetwitter/andreareginato
  • 4. Just a little introduction RSpec is a library that focus ontesting the behavior of youreveryday projecttwitter/andreareginato
  • 5. Why people do not use it Probably the testing practice is themost diffcult forma mentis adeveloper have to learn twitter/andreareginato
  • 6. It will save our life, maybeIn the long term, you will not checkyour web page everytime you make a new feature, but you willcheck if your test passedtwitter/andreareginato
  • 7. Short descriptions# wrongit "should have 422 status code if anunexpected params will be added" doresponse.should respond_with 422end# correctcontext "when not valid"it { should respond_with 422 }
  • 8. (over)use contextsand use with/when as keys# wrongit "should have 200 status code iflogged in" doresponse.should respond_with 200end# correctcontext "when logged out" doit { should respond_with 401 }end
  • 9. Describe methods# wrongdescribe "the authenticate method for User logged in" ...describe "if the user is an admin" do# correctdescribe ".authenticate" dodescribe "#admin?"
  • 10. Single expectation test# wrongit "should create a resource" doresponse.should respond_with_content_type(:json)response.should assign_to(:resource)end# correctit { should respond_with_content_type(:json) }it { should assign_to(:resource) }
  • 11. Test the edge cases# sample actiondef [email protected] = Resource.where(:id => params[:id])if @[email protected] 204elserender :template => "shared/404", :status => 404,endendWhat would you test ?
  • 12. Test the edge case# correctdescribe "#destroy" docontext "when resource is found" doit "should render_with 204"it "should assign @resource"context "when resource is not found" doit "should render with 404"it "should not assign @resource"endendIs this enough ?
  • 13. Use the subject# wrongit { assigns("message").should match /The resource name is Genoveffa/ }it { assigns("message").should match /it was born in Billyville/ }it { assigns("message").creator.should match /Claudiano/ }# correctsubject { assigns("message") }it { should match /The resource name is Genoveffa/ }it { should match /it was born in Billyville/ }its(:creator) { should match /Claudiano/ }
  • 14. Mock or not to mock# wrong (a really personal point of view)1) Its wrong when you never mock and recreate by codea “medium complex” situation and its time consuming2) Its wrong when you mock everything. You have a lightsystem and the big advantage of independence, but you loosepart of the control on your application.# correct# simulate authenticated usercontroller.stub!(:authenticate).and_return(true)# simulate current usercontroller.stub(:current_user).and_return(current_user)# simulate not found recordResource.stub(:where).with(created_from: id) .and_return(false)
  • 15. Create data when needed# wrongYou shouldnt use fixtures* loose control on our data* load everything all the time* difficult to find problems# correctdescribe "User"describe ".top" dobefore { 3.times { Factory(:user) } }it { User.top(2).should have(2).item }endend
  • 16. Shared examples to DRY# wrongcontext "when own resources" doit "should have it" doresource = Factory("user")do_get format: jsonassigns(users).should include(resource)endendcontext "when does not own resource" doit "should not have it" donot_owned_resource = Factory("unknown")do_get format: jsonassigns(users).should_not include(not_owned_resource)endend
  • 17. Shared examples to DRY# correct (but why?)shared_examples for "a secure resource" docontext "when own the resource" doit "should have it" doresource = Factory("user")do_get format: jsonassigns(users).should include(resource)endendcontext "when does not own resource" doit "should not have it" donot_owned_resource = Factory("unknown")do_get format: jsonassigns(users).should_not include(not_owned_resource)endendend
  • 18. Shared examples to DRY# correct (apply DRY on tests)describe "#show" doit_should_behave_like "a secure resource"it_should_behave_like "a findable resource"it_should_behave_like "a secure resource"context "when not logged in" doit_should_handle "a not authenticated request"endend
  • 19. More?# wrongDo not use RSpec# correctStart using RSpec 2 into your next project
  • 20. Thanks# more infoThere is a public google doc that Ive started with some friends,and whenever you want you can write me# [email protected]/andreareginato
  • Fly UP