service objects in rails tests - factory_girl replacement

34
Service objects as a way of testing Rails apps @andrzejkrzywda @arkency In love/hate relationship with Rails, since 2004

Upload: andrzej-krzywda

Post on 11-Jul-2015

511 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Service objects in Rails tests - factory_girl replacement

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

Service objects

Page 3: Service objects in Rails tests - factory_girl replacement

The things you call from your controllers

Page 4: Service objects in Rails tests - factory_girl replacement

Rails is not your app Your app is below controllers

Page 5: Service objects in Rails tests - factory_girl replacement

Most of the things without HTTP

Page 6: Service objects in Rails tests - factory_girl replacement

AddProductToCart ChangeAmount

CreateNewBlogPost PublishBlogPost

AssignTestToPupils CreateNewKlass CheckoutOrder LogTimeEntry

ChangeInsurancePolicy

Page 7: Service objects in Rails tests - factory_girl replacement

http://blog.arkency.com/2014/02/rails-refactoring-the-aha-moments/

Page 8: Service objects in Rails tests - factory_girl replacement

Things that “do” something

Page 9: Service objects in Rails tests - factory_girl replacement

Query objects - read the system

Page 10: Service objects in Rails tests - factory_girl replacement

ListOfOrders UserQuery

PupilsInKlass BlogPostsByAuthor

Page 11: Service objects in Rails tests - factory_girl replacement

Reads are easy Commands are more

difficult

Page 12: Service objects in Rails tests - factory_girl replacement

Rails-way is good for Reads

Page 13: Service objects in Rails tests - factory_girl replacement

Rails-way is not ideal for Commands

Page 14: Service objects in Rails tests - factory_girl replacement

Testing

Page 15: Service objects in Rails tests - factory_girl replacement

System tests vs

Unit tests

Page 16: Service objects in Rails tests - factory_girl replacement

Good tests tell a story

Page 17: Service objects in Rails tests - factory_girl replacement

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

How to setup the state?

Page 19: Service objects in Rails tests - factory_girl replacement

factory_girl

Page 20: Service objects in Rails tests - factory_girl replacement

Service objects in tests

Page 21: Service objects in Rails tests - factory_girl replacement

What makes a good unit?

Page 22: Service objects in Rails tests - factory_girl replacement

Unit == a set of service objects

Page 23: Service objects in Rails tests - factory_girl replacement

scenario == service objects

Page 24: Service objects in Rails tests - factory_girl replacement

Wins?

Page 25: Service objects in Rails tests - factory_girl replacement

hides implementation details

Page 26: Service objects in Rails tests - factory_girl replacement

tests in sync with production code

Page 27: Service objects in Rails tests - factory_girl replacement

Easy to write

Page 28: Service objects in Rails tests - factory_girl replacement

Fast

Page 29: Service objects in Rails tests - factory_girl replacement

Problems?

Page 30: Service objects in Rails tests - factory_girl replacement

Tests are one layer below !

Easy to make mistakes when calling services from controllers

Page 31: Service objects in Rails tests - factory_girl replacement

Not so easy to extract services from an existing,

legacy Rails codebase

Page 32: Service objects in Rails tests - factory_girl replacement

Examples

Page 33: Service objects in Rails tests - factory_girl replacement

http://blog.arkency.com/2014/06/setup-your-tests-with-services/

Page 34: Service objects in Rails tests - factory_girl replacement

Thanks!