5 ms·
You're right that it's similar to extracting it to a module since (in this example) HashGenerator.new is evaluated with the class and in my Request spec I can't
by nagnatron 15y ago
You're right that it's similar to extracting it to a module since (in this example) HashGenerator.new is evaluated with the class and in my Request spec I can't exactly stub HashGenerator.new to return a double.
How would you do it?
- camwest 15y agoCreate a RequestFactory which is responsible for creating valid requests, put the hash generator in there. Then use the factory in the controller instead of directly creating the model. That way you don't have the indirection of an ActiveRecord callback and you have your process of creating a request and assigning it a unique hash in a straightforward single process.
- JonWood 15y agoI'm starting to use service objects, which are responsible for coordinating things when multiple models are required to perform an operation, or complex business logic is involved. James Golick has a great blog post on that approach at http://jamesgolick.com/2010/3/14/crazy-heretical-and-awesome-the-way-i-write-rails-apps.html http://jamesgolick.com/2010/3/14/crazy-heretical-and-awesome...