RSpec Tips November 12, 2012
RSpec Ruby testing framework Friendlier alternative to Test::Unit.
RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based
RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based Syntax resembles ...
RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based Syntax resembles ...
Test Runner Database initialization $ rake db:create RAILS_ENV=test $ rake db:reset RAILS_ENV=test Running the test suite ...
Test Runner (cont.)  Example: truncate databases only on startup  Viewing code coverage  coverage/index.html  Auto-testing...
Test Structure  Example Groups    describe    context
Test Structure  Example Groups    describe    context  Examples    it
Test Structure  Example Groups    describe    context  Examples    it  Assertions    should    should_not
Test Structure  Example Groups    describe    context  Examples    it  Assertions    should    should_not  BDD w/ Pending ...
Test Structure (cont.)  Matchers    be_nil, be_true, be_false    == vs eql    have_key    change    should_receive    ... ...
Test Structure (cont.)  Matchers    be_nil, be_true, be_false    == vs eql    have_key    change    should_receive    ... ...
Hooks Callbacks used to setup/teardown tests
Hooks Callbacks used to setup/teardown tests Kinds?
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Hooks Callbacks used to setup/teardown tests Kinds?     before :suite     before :all     before :each     around :each   ...
Subject The main object of concern inside an example group Implicit vs. Explicit
Subject (cont.)  should without a receiver  its shortcut  Concise + Consistent = DRY (specs as documentation)  ”One expect...
Unit Tests  Checking piece-by-piece
Unit Tests  Checking piece-by-piece  Isolate
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)    Remove dependencies (o...
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)    Remove dependencies (o...
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)    Remove dependencies (o...
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)    Remove dependencies (o...
Unit Tests  Checking piece-by-piece  Isolate    Design idempotent tests (no database truncation)    Remove dependencies (o...
Integration Tests  Putting the pieces together and making  sure they fit
Integration Tests  Putting the pieces together and making  sure they fit  Simulating real users with Capybara
Integration Tests  Putting the pieces together and making  sure they fit  Simulating real users with Capybara  Mocking auth...
Integration Tests  Putting the pieces together and making  sure they fit  Simulating real users with Capybara  Mocking auth...
Integration Tests  Putting the pieces together and making  sure they fit  Simulating real users with Capybara  Mocking auth...
Additional Resources Pragmatic, The RSpec Book http://betterspecs.org http://rspec.info https://www.relishapp.com/rspec ht...
Upcoming SlideShare
Loading in …5
×

Rspec Tips

2,395 views

Published on

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,395
On SlideShare
0
From Embeds
0
Number of Embeds
757
Actions
Shares
0
Downloads
16
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Rspec Tips

    1. 1. RSpec Tips November 12, 2012
    2. 2. RSpec Ruby testing framework Friendlier alternative to Test::Unit.
    3. 3. RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based
    4. 4. RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based Syntax resembles natural language describe.. it..
    5. 5. RSpec Ruby testing framework Friendlier alternative to Test::Unit. Behavior-driven development TDD-based Syntax resembles natural language describe.. it.. Good as documentation Where BDD shines.
    6. 6. Test Runner Database initialization $ rake db:create RAILS_ENV=test $ rake db:reset RAILS_ENV=test Running the test suite $ rspec spec Configuration .rspec spec/spec_helper.rb
    7. 7. Test Runner (cont.) Example: truncate databases only on startup Viewing code coverage coverage/index.html Auto-testing with Guard $ bundle exec guard Spork $ spork
    8. 8. Test Structure Example Groups describe context
    9. 9. Test Structure Example Groups describe context Examples it
    10. 10. Test Structure Example Groups describe context Examples it Assertions should should_not
    11. 11. Test Structure Example Groups describe context Examples it Assertions should should_not BDD w/ Pending Tests pending
    12. 12. Test Structure (cont.) Matchers be_nil, be_true, be_false == vs eql have_key change should_receive ... etc
    13. 13. Test Structure (cont.) Matchers be_nil, be_true, be_false == vs eql have_key change should_receive ... etc Custom Matchers (shoulda-matchers) should allow_mass_assignment_of :some_attribute should have_many(:association).dependent(:destroy) should validate_presence_of :first_name
    14. 14. Hooks Callbacks used to setup/teardown tests
    15. 15. Hooks Callbacks used to setup/teardown tests Kinds?
    16. 16. Hooks Callbacks used to setup/teardown tests Kinds? before :suite
    17. 17. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all
    18. 18. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each
    19. 19. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each
    20. 20. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each
    21. 21. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all
    22. 22. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all after :suite
    23. 23. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all after :suite RSpec’s default behavior
    24. 24. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all after :suite RSpec’s default behavior before :suite = Database truncation
    25. 25. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all after :suite RSpec’s default behavior before :suite = Database truncation after :each = Transaction rollback config.use_transactional_fixtures = true
    26. 26. Hooks Callbacks used to setup/teardown tests Kinds? before :suite before :all before :each around :each after :each after :all after :suite RSpec’s default behavior before :suite = Database truncation after :each = Transaction rollback config.use_transactional_fixtures = true The problem with using multiple databases
    27. 27. Subject The main object of concern inside an example group Implicit vs. Explicit
    28. 28. Subject (cont.) should without a receiver its shortcut Concise + Consistent = DRY (specs as documentation) ”One expectation” rule Example: Project template before & after
    29. 29. Unit Tests Checking piece-by-piece
    30. 30. Unit Tests Checking piece-by-piece Isolate
    31. 31. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation)
    32. 32. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation) Remove dependencies (openfire)
    33. 33. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation) Remove dependencies (openfire) Narrow down responsibility (template inheritance)
    34. 34. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation) Remove dependencies (openfire) Narrow down responsibility (template inheritance) Trust but verify
    35. 35. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation) Remove dependencies (openfire) Narrow down responsibility (template inheritance) Trust but verify shoulda-matchers
    36. 36. Unit Tests Checking piece-by-piece Isolate Design idempotent tests (no database truncation) Remove dependencies (openfire) Narrow down responsibility (template inheritance) Trust but verify shoulda-matchers Mock and stub (omniauth_callbacks_controller_spec.rb)
    37. 37. Integration Tests Putting the pieces together and making sure they fit
    38. 38. Integration Tests Putting the pieces together and making sure they fit Simulating real users with Capybara
    39. 39. Integration Tests Putting the pieces together and making sure they fit Simulating real users with Capybara Mocking authentication
    40. 40. Integration Tests Putting the pieces together and making sure they fit Simulating real users with Capybara Mocking authentication Devise::TestHelpers (problem with spec controllers bypassing the router, problem with lack of direct access to the request variable in integration tests)
    41. 41. Integration Tests Putting the pieces together and making sure they fit Simulating real users with Capybara Mocking authentication Devise::TestHelpers (problem with spec controllers bypassing the router, problem with lack of direct access to the request variable in integration tests) Warden::Test::Helpers
    42. 42. Additional Resources Pragmatic, The RSpec Book http://betterspecs.org http://rspec.info https://www.relishapp.com/rspec https://github.com/bbatsov/ruby-style-guide http://mikbe.tk/2011/03/21/mocks-stubs- rspec

    ×