service objects in rails tests - factory_girl replacement
TRANSCRIPT
![Page 1: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/1.jpg)
Service objects as a way of testing Rails apps
@andrzejkrzywda @arkency
In love/hate relationship with Rails, since 2004
![Page 2: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/2.jpg)
Service objects
![Page 3: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/3.jpg)
The things you call from your controllers
![Page 4: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/4.jpg)
Rails is not your app Your app is below controllers
![Page 5: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/5.jpg)
Most of the things without HTTP
![Page 6: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/6.jpg)
AddProductToCart ChangeAmount
CreateNewBlogPost PublishBlogPost
AssignTestToPupils CreateNewKlass CheckoutOrder LogTimeEntry
ChangeInsurancePolicy
![Page 7: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/7.jpg)
http://blog.arkency.com/2014/02/rails-refactoring-the-aha-moments/
![Page 8: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/8.jpg)
Things that “do” something
![Page 9: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/9.jpg)
Query objects - read the system
![Page 10: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/10.jpg)
ListOfOrders UserQuery
PupilsInKlass BlogPostsByAuthor
![Page 11: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/11.jpg)
Reads are easy Commands are more
difficult
![Page 12: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/12.jpg)
Rails-way is good for Reads
![Page 13: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/13.jpg)
Rails-way is not ideal for Commands
![Page 14: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/14.jpg)
Testing
![Page 15: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/15.jpg)
System tests vs
Unit tests
![Page 16: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/16.jpg)
Good tests tell a story
![Page 17: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/17.jpg)
1. User adds a product to the cart 2. User looks at the cart to see the current total amount
3. User changes the amount 4. User goes to checkout
![Page 18: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/18.jpg)
How to setup the state?
![Page 19: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/19.jpg)
factory_girl
![Page 20: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/20.jpg)
Service objects in tests
![Page 21: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/21.jpg)
What makes a good unit?
![Page 22: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/22.jpg)
Unit == a set of service objects
![Page 23: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/23.jpg)
scenario == service objects
![Page 24: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/24.jpg)
Wins?
![Page 25: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/25.jpg)
hides implementation details
![Page 26: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/26.jpg)
tests in sync with production code
![Page 27: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/27.jpg)
Easy to write
![Page 28: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/28.jpg)
Fast
![Page 29: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/29.jpg)
Problems?
![Page 30: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/30.jpg)
Tests are one layer below !
Easy to make mistakes when calling services from controllers
![Page 31: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/31.jpg)
Not so easy to extract services from an existing,
legacy Rails codebase
![Page 32: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/32.jpg)
Examples
![Page 33: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/33.jpg)
http://blog.arkency.com/2014/06/setup-your-tests-with-services/
![Page 34: Service objects in Rails tests - factory_girl replacement](https://reader035.vdocument.in/reader035/viewer/2022081401/55a061571a28ab372e8b47cc/html5/thumbnails/34.jpg)
Thanks!