DEV Community

Discussion on: Bypassing the database and testing to see if this solution works

Collapse
 
bhserna profile image
Benito Serna • Edited

Hi @chenge , thanks for sharing my article =) ... although apparently the link is broken dev.to/bhserna/how-to-test-that-yo...

About the verification code...

I used the rspec test to be feasible, but the problem is that the verification code is not executed after the simulation, I don't know how to solve it.

I think that what Leonardo said is a good solution... I prefer the other just because you don't need to write allow(store).to receive(:create) before.

Other option is to create a custom "spy" that can do whatever you want, like...

class StoreSpy
  attr_reader :create_messages

  def initialize
    @create_messages = []
  end

  def create(attrs)
    create_messages << attrs
  end
end

store = StoreSpy.new

Catalog.add_product(attrs, store)
expect(store.create_messages).to include { name: "P1", description: "Super Product" }

#or

Catalog.add_product(attrs, store)
expect(store.create_messages).to eq [{ name: "P1", description: "Super Product" }]

But I think that in this case I prefer the other two solutions.

Abut the clean architecture reference...

I feel that this code is to make the model as the entity, the corresponding Product class and class method as the database interface. Closer to the architectural idea of ​​the illustration. The original rails are organized with data as the center, or the database is the center.

Yes! this is more or less what I do and propose... To use the model more or less like an entity and the Product class as the Database interface.