Rspec

519 views

Published on

Quick Reference of Rspec Techniques

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

  • Be the first to like this

No Downloads
Views
Total views
519
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rspec

  1. 1. Rspec & Factory Girl Geeta Vinnakota 7/19/2013
  2. 2. Organization ● Specs are organized in 'describe' and 'it' blocks ● Before & after hooks ● An example is nothing but an 'it' block
  3. 3. Helper Methods ● Let – lazy evaluated, ie not evaluated until the first call. The value is cached in a 'it' block ● Let! Force invocated before each 'it' block
  4. 4. Model Specs ● Spec for each class & instance method ● Call the method & match the response with the expected value ● Use Factory data to create objects to test against
  5. 5. Controller Specs ● Simulate a http request ● Views are stubbed by default. If you are using jbuilder or similar template on the api, use 'render_views' to render the views ● Also pass in the format parameter, in the request ● Check for response code, response data etc
  6. 6. Expecations ● Every object in Rspec has 2 methods should, should_not ● They accept a matcher as an argument ( matcher can act in a positive or negative mode) ● https://www.relishapp.com/rspec/rspec- expectations/v/2-13/docs/built-in-matchers
  7. 7. Mocks(Mocha) ● Mocha is the preferred framework for mocking ● Expects, with, returns ● mock_obj = Mock(obj) ● Stubs & returns on mock objects ● Set the expectation & then make the call
  8. 8. Command Line Options ● --fail-fast to exit after the first failue ● :789 - runs the spec in that line
  9. 9. FactoryGirl ● Test it on console ● require 'factory_girl' ● require 'factory_girl_rails' ● FactoryGirl.find_definitions
  10. 10. Creating Associations Using Factories ● Model belongs to another model, it contains the id of the other model.
  11. 11. When to mock & stub ● To fill up parameters in a method call ● 3rd party calls ●

×