3. RSpec
Ruby testing framework
Friendlier alternative to Test::Unit.
Behavior-driven development
TDD-based
4. RSpec
Ruby testing framework
Friendlier alternative to Test::Unit.
Behavior-driven development
TDD-based
Syntax resembles natural language
describe.. it..
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. 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. Test Runner (cont.)
Example: truncate databases only on startup
Viewing code coverage
coverage/index.html
Auto-testing with Guard
$ bundle exec guard
Spork
$ spork
18. Hooks
Callbacks used to setup/teardown tests
Kinds?
before :suite
before :all
before :each
19. Hooks
Callbacks used to setup/teardown tests
Kinds?
before :suite
before :all
before :each
around :each
20. Hooks
Callbacks used to setup/teardown tests
Kinds?
before :suite
before :all
before :each
around :each
after :each
21. Hooks
Callbacks used to setup/teardown tests
Kinds?
before :suite
before :all
before :each
around :each
after :each
after :all
22. Hooks
Callbacks used to setup/teardown tests
Kinds?
before :suite
before :all
before :each
around :each
after :each
after :all
after :suite
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. 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. 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. 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. Subject
The main object of concern inside an
example group
Implicit
vs.
Explicit
28. Subject (cont.)
should without a receiver
its shortcut
Concise + Consistent = DRY (specs as documentation)
”One expectation” rule
Example: Project template before & after
31. Unit Tests
Checking piece-by-piece
Isolate
Design idempotent tests (no database truncation)
32. Unit Tests
Checking piece-by-piece
Isolate
Design idempotent tests (no database truncation)
Remove dependencies (openfire)
33. Unit Tests
Checking piece-by-piece
Isolate
Design idempotent tests (no database truncation)
Remove dependencies (openfire)
Narrow down responsibility (template inheritance)
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. 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. 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. Integration Tests
Putting the pieces together and making
sure they fit
38. Integration Tests
Putting the pieces together and making
sure they fit
Simulating real users with Capybara
39. Integration Tests
Putting the pieces together and making
sure they fit
Simulating real users with Capybara
Mocking authentication
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. 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. 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