DEV Community

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

Collapse
 
leonardocouy profile image
Leonardo Couy

Nice post!!! That's unit test!! =DD

I'll give another way to test, but, using the rspec matcher have_received because it seems more readable. See the example below using have_received matcher and tell me what do you think, ok? =)

  it "creates a record" do
    ##### Setup
    attrs = { name: "P1", description: "Super Product" }
    allow(store).to receive(:create)

    ##### Execution
    Catalog.add_product(attrs, store)

    ##### Expectation
    expect(store).to have_received(:create).with(attrs)
  end

see more about have_received matcher

Collapse
 
chenge profile image
chenge

Thanks for reply. Here my main focus is how to run validation code while testing.

I'm a new comer for rspec so I cannot say too much about it.