I’m not a big fan of ActiveRecord callbacks – it usually seems like a quick-and-easy solution, but in the long term it makes testing difficult and can often create unexpected side-effects. However, sometimes there really is no other option which means we need to test these callbacks!
One way of testing the callbacks is to invoke the run_callbacks method.
However, this no longer seems to work in the latest version of RSpec – you will receive this error: NoMethodError: undefined method `_run_before_save_callbacks’
The API has changed slightly – you can now either run only the before_save callbacks, or run both the before_save and after_save callbacks.
To run only the before_save callbacks:
To run both the before_save and after_save callbacks:
However, I would argue that invoking the individual callbacks in this way is not the best way of testing the callbacks. I much rather prefer to define a private method for the callback and then invoking the private method.
In this case, we can test the create_price method individually - still messy, but cleaner in my opinion.
Of course, the best solution is to avoid callbacks altogether. Happy coding.