RSpec 2 Best practices
Upcoming SlideShare
Loading in...5
×
 

RSpec 2 Best practices

on

  • 382,525 views

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

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

Statistics

Views

Total Views
382,525
Views on SlideShare
23,258
Embed Views
359,267

Actions

Likes
62
Downloads
269
Comments
1

80 Embeds 359,267

http://betterspecs.org 357560
http://localhost 486
http://www.betterspecs.org 345
http://0.0.0.0 153
http://senthilnambi.posterous.com 103
http://italianstestitbetter.herokuapp.com 81
https://helpified.com 48
http://plus.url.google.com 41
http://www.helpified.com 40
http://tweets.joshrobinson.com 38
http://www.twylah.com 37
http://translate.googleusercontent.com 36
http://webcache.googleusercontent.com 32
http://www.atompad.com 29
http://www.tuicool.com 18
https://www.google.com 17
http://videofork.com 17
http://blog.mikamai.com 16
http://betterspecs.org. 15
http://www.google.com 14
https://www.facebook.com 13
http://zootool.com 11
http://118.130.116.75 7
http://betterspecs.dev 7
https://si0.twimg.com 7
https://twitter.com 7
http://192.168.2.98 7
https://www.google.ae 4
http://www.google.ru 3
http://a0.twimg.com 3
http://paper.li 3
http://cache.preserve.io 3
https://www.google.ru 3
https://www.google.com.au 2
http://www.google.co.uk 2
https://www.google.es 2
https://www.google.co.il 2
https://translate.googleusercontent.com 2
http://wtanaka.com 2
http://www.google.co.jp 2
http://prlog.ru 2
https://www.google.co.uk 2
https://www.google.de 2
http://abtasty.com 2
http://docs.in 2
http://www.opentapestry.dev 2
http://twitter.com 2
http://s.deeeki.com 2
http://tweets.mediumequalsmessage.com 2
http://tweetedtimes.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

RSpec 2 Best practices RSpec 2 Best practices Presentation Transcript

  • Rspec Best Practices Ruby Social Club Milano 24 February 2011 Andrea Reginato twitter/andreareginato
  • Ill not teach RSpecYou have good books and web sites The RSpec Book Rails Test Prescriptions twitter/andreareginato
  • Ill tell you how I use RSpec What Ive learned reading books some articles on the web but most important from my everyday job experience twitter/andreareginato
  • Just a little introduction RSpec is a library that focus on testing the behavior of your everyday project twitter/andreareginato
  • Why people do not use it Probably the testing practice is the most diffcult forma mentis a developer have to learn twitter/andreareginato
  • It will save our life, maybe In the long term, you will not check your web page everytime you make a new feature, but you will check if your test passed twitter/andreareginato
  • Short descriptions# wrongit "should have 422 status code if an unexpected params will be added" do response.should respond_with 422end# correctcontext "when not valid" it { should respond_with 422 }
  • (over)use contexts and use with/when as keys# wrongit "should have 200 status code if logged in" do response.should respond_with 200end# correctcontext "when logged out" do it { should respond_with 401 }end
  • Describe methods# wrongdescribe "the authenticate method for User logged in" ...describe "if the user is an admin" do# correctdescribe ".authenticate" dodescribe "#admin?"
  • Single expectation test# wrongit "should create a resource" do response.should respond_with_content_type(:json) response.should assign_to(:resource)end# correctit { should respond_with_content_type(:json) }it { should assign_to(:resource) }
  • Test the edge cases# sample actiondef destroy @resource = Resource.where(:id => params[:id]) if @resource @resource.destroy head 204 else render :template => "shared/404", :status => 404, endendWhat would you test ?
  • Test the edge case# correctdescribe "#destroy" do context "when resource is found" do it "should render_with 204" it "should assign @resource" context "when resource is not found" do it "should render with 404" it "should not assign @resource" endendIs this enough ?
  • 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/ }
  • 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)
  • 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" do before { 3.times { Factory(:user) } } it { User.top(2).should have(2).item } endend
  • Shared examples to DRY# wrongcontext "when own resources" do it "should have it" do resource = Factory("user") do_get format: json assigns(users).should include(resource) endendcontext "when does not own resource" do it "should not have it" do not_owned_resource = Factory("unknown") do_get format: json assigns(users).should_not include(not_owned_resource) endend
  • Shared examples to DRY# correct (but why?)shared_examples for "a secure resource" do context "when own the resource" do it "should have it" do resource = Factory("user") do_get format: json assigns(users).should include(resource) end end context "when does not own resource" do it "should not have it" do not_owned_resource = Factory("unknown") do_get format: json assigns(users).should_not include(not_owned_resource) end endend
  • Shared examples to DRY# correct (apply DRY on tests)describe "#show" do it_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" do it_should_handle "a not authenticated request" endend
  • More?# wrongDo not use RSpec# correctStart using RSpec 2 into your next project
  • Thanks# more infoThere is a public google doc that Ive started with some friends,and whenever you want you can write me# contactsandrea.regianto@gmail.comtwitter/andreareginato