Test on Rails
based on “Rails Test Prescriptions” by Noel Rappin
Ryo Nakamura (@r7kamura) ✘ ╹◡╹ ✘
Slide 2
Slide 2 text
1. Why test?
2. Unit
3. Model
4. Functional
5. View
6. Integration
Slide 3
Slide 3 text
Why test?
Slide 4
Slide 4 text
* to speed up validation
* to design architectures
* to assure its quality
...
Why test?
Slide 5
Slide 5 text
to speed up validation
automating test
* open browser...
* type the URL “h..tt..p://...”
* check the contents...
Slide 6
Slide 6 text
to design architecture
* write clean and independent code
(test behaves like a third-party code)
* think the correct behavior of code
(this would be an example of test)
Slide 7
Slide 7 text
to assure its quality
* prevent code from enbugging
* maybe forced to write test ... ;(
Slide 8
Slide 8 text
For more details why you...
Please ask Mohicans around you
Slide 9
Slide 9 text
Unit Test
Slide 10
Slide 10 text
Forms of writing unit test - ✘╹◡╹ ✘
http://r7kamura.hatenablog.com/
entry/2012/09/19/113239
Unit Test
Slide 11
Slide 11 text
Model Test
Slide 12
Slide 12 text
Unit Test of Model.
It's easy to write Model Test.
"Skinny Controller, Fat Model"
Model Test
Slide 13
Slide 13 text
Functional Test
Slide 14
Slide 14 text
Functional tests
Verify the system satisfies
requested features of user.
In Rails, it tests Controller & View
by 1 request
Slide 15
Slide 15 text
Targets
1. normal
2. invalid
3. security
Slide 16
Slide 16 text
1. normal
A normal request should pass
expected data to the view.
@foo, sessions, cookies, flash
Slide 17
Slide 17 text
2. invalid
An invalid request should
be handled properly.
redirect_to :root
Slide 18
Slide 18 text
3. security
User’s and system’s security
should be protected.
get(others_entry_path).
should_not be_success
Slide 19
Slide 19 text
Non targets
* many redirections
* user interaction
* HTTP methods (in Rails)
Slide 20
Slide 20 text
View Test
Slide 21
Slide 21 text
View Test
Verify:
* logic
* structure (semantic mean)
Used in Controller & Integration Test on Rails
Slide 22
Slide 22 text
Good targets
* logic in a view
(necessary evil...;)
* security problem
* way to render structures
Slide 23
Slide 23 text
Bad targets
* text in HTML tag
* look and feel
verify only “semantic meanings”
Slide 24
Slide 24 text
Integration Test
Slide 25
Slide 25 text
Integration Test
verify the interactions(I/F) of
multiple components
Acceptance test might see to this.
Capybara, Cucumber, ...
Slide 26
Slide 26 text
When to use Integration Test?
To validate the interactions of
the modules that are well-tested.
Slide 27
Slide 27 text
Good targets
* interactions in multiple steps
* dependency with prev state
* legacy code (black-box)
* with JavaScript
Slide 28
Slide 28 text
Bad targets
* only 1 request
* only for view testing
Slide 29
Slide 29 text
1. Why test?
2. Unit
3. Model
4. Functional
5. View
6. Integration