Your SlideShare is downloading. ×
0
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
RSpec and Rails
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 and Rails

5,277

Published on

Presentation on using RSpec with Rails. Assumes basic RSpec knowledge. …

Presentation on using RSpec with Rails. Assumes basic RSpec knowledge.

Given

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,277
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
65
Comments
0
Likes
3
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 and Rails<br />Alan Hecht<br />http://alanhecht.me<br />
  • 2. Installation<br />RSpec 2.5<br />Only works with Rails 3<br />rspec and rspec-rails<br />gem install rspec-rails<br />What if you’re using Rails 2.x?<br />Need to install RSpec 1.3<br />
  • 3. Creating a Rails Project<br />rails new cart_rspec<br />Edit the Gemfile<br />bundle install<br />rails generate rspec:install<br />
  • 4. Railswizard.org<br />Railswizard.org allows you to specify options when creating a Rails 3 project<br />Rails 3 allows for flexibility in what’s used<br />Database/ORM <br />ActiveRecord, Mongomapper<br />Unit Testing Framework<br />TestUnit, Rspec<br />JavaScript Framework<br />jQuery, Prototype<br />
  • 5. Set up the project<br />Use railswizard to create a template<br />We’ll be creating the shopping cart<br />Create rvmrc file<br />bundle install<br />Git setup<br />
  • 6. Set up the Product & Cart<br />Generate product scaffolding<br />Generate cart scaffolding<br />Generate line_item scaffolding<br />RSpec tests were created with scaffolding<br />Commit changes<br />
  • 7. RSpec - Models<br />
  • 8. Run Tests<br />Update database<br />rake db:migrate<br />Run RSpec<br />rake spec<br />
  • 9. RSpec Options<br />In .rspec file<br />Documentation format<br />--format doc<br />HTML format<br />--format html:/path/to/file<br />
  • 10. RSpec – Models<br />Most similar to using RSpec without Rails<br />Everything we learned from the calculator example applies<br />Business logic testing should take place in the models <br />
  • 11. RSpec - Models<br />
  • 12. RSpec – Fixtures<br />Fixture files go in spec/fixtures<br />Need to explicitly load fixtures <br />Can load all the fixtures for tests<br />Add ‘config.global_fixtures = true’ to spec_helper.rb<br />
  • 13. RSpec - Controllers<br />In Rails Test::Unit, Controllers and Views tested together in functional tests<br />For RSpec, each is tested separately<br />Outer describe contains the controller class<br />Needed to run the tests<br />Describe in each test has controller action<br />Doesn’t affect the test, only by convention<br />
  • 14. Controller Matchers<br />RSpec has matchers specifically for controller testing:<br />response.should be_success<br />response.should be_redirect<br />response.should redirect_to(url)<br />response.should render_template(‘app/views/products/index’)<br />response.should render_template(:partial => “show_cart”)<br />
  • 15. Controllers - Example<br />
  • 16. Stubs – What are they?<br />A fake object that returns a pre-determined value for a method call<br />Actual object is not called<br />Stubs and mocks often used interchangeably<br />Mean two different things<br />But people generally refer to “Mock Object Frameworks”<br />
  • 17. Stubs – Why?<br />Separation of concerns<br />Controller doesn’t depend on model<br />Models don’t depend on each other<br />Each model can be tested in isolation<br />Don’t need the database for testing<br />Faster tests<br />Could use stubs to simulate a network or web service response<br />
  • 18. Stubs – Implications<br />Need to have integration tests<br />All of your tests could pass, but the site may not function<br />Webrat (used by Cucumber) & Capybara are two such integration tools<br />
  • 19. Mocks<br />Just like Stubs, but with an expectation to be (or not to be) called<br />Test will fail if the mock is (or isn’t) called<br />Used in the same way as stubs<br />
  • 20. RSpec - Views<br />Verify the presence of fields on the view<br />Avoid relying on the layout of the view<br />View could break from a design change<br />But good for testing groups of elements with a common purpose (like options fields that expand or hide)<br />Use XPath or CSS selectors to locate tags<br />Strongly recommend CSS selectors<br />
  • 21. RSpec - Views<br />
  • 22. RSpec - Views<br />Describe contains view path<br />Optional but useful<br />If ‘render’ doesn’t contain arguments, use the comment on the describe line<br />
  • 23. Test Coverage<br />rcov gem will show you how much of your code has been tested<br />rake spec:rcov<br />By default, all tests are run<br />Need to modify Rakefile to cover only specific areas like models & views<br />Output is in the <project>/coverage directory<br />HTML viewable in a browser<br />Open coverage/index.html<br />
  • 24. RCov - Output<br />
  • 25. RCov - Tips<br />No need to try for 100% test coverage<br />Good goal would be around 70 – 80%<br />
  • 26. Automatic Testing<br />Autotest monitors changes to files in your project<br />Every time a test is saved, the test is re-run<br />Everytime app code is saved, corresponding test is run<br />gem install autotest & autotest-rails-pure<br />Create an autotest directory<br />discover.rb<br />
  • 27. Autotest – Tips<br />Be careful when using on a laptop<br />Autotest polls for changes<br />You’ll either get burnt by a hot laptop or your battery will die, whichever comes first<br />On OS X, you can install autotest-fsevent, the file system change triggers autotest execution<br />Growl notifications<br />autotest-growl<br />Add to rails root .autotest file<br />

×