DEV Community

Let's expect change and reload!

Szymon Lipka on April 03, 2023

It's often that we create a service that is supposed to change an attribute on an ActiveRecord object. Testing such a service is tricky at times. ...
Collapse
 
katafrakt profile image
Paweł Świątkowski • Edited

Is the service implemented correctly? Obviously not! It will not save changes to the database that are done, which is expected!

I don't know, I wouldn't say that it's that obvious. The service name just hints that it updates the user's name, it does not say anything about persisting the change in the database. I can easily imagine a service that updates a shopping cart's total price using a set of complex business rules and returns a dirty AR object with which you can do whatever you want, including persisting the change or piping it to another service that calculates expected delivery date.

If the service does not talk to the database, it's also easier to tests, as we see.

So while probably most services of this kind would actually persist the change in the database, it's the convention of a particular codebase whether it's "obvious" or not ;)

Collapse
 
szymonlipka profile image
Szymon Lipka

Makes sense! In this particular case we expect it to persist, so I have rephrased it. Thanks!