Integration Test

We're adding an integration test first so that we can have something against which to develop as we build out this new feature. This early on, the design could still be in flux. We want to allow ourselves flexibility as well as confidence that we don't break what we just built.

Write out your basic happy path expectations

  • Let's break this into chunks and start with initiate
  • Write out your expectations of side effects in your happy path case
it "happy path" do
  # Initiating a request creates a book request for the cat
  # Sends a notification to the available cat reading wranglers
end

Think about your dependencies

  • To initiate a request, we need a cat and some request data...
let(:kitty) { FactoryBot.create(:cat) }

let(:request_data) do
  {
    genre: "romance",
    num_books: 5,
    urgency: "asap"
  }
end

Write out your basic expectations to test behavior

In our integration test, we want to test our high level data side effects. We want to test how our pieces fit together.

it "happy path" do
  # Initiating a request creates a book request for the cat
  expect{ ReadingRequests::Initiate.call(cat: kitty, request_data: request_data) }
    .to change{ kitty.book_requests.count }
    .by(1) # initiating a reading request should only create one book request for cat
end

Does our test pass?

Nope.

Failures:

  1) Reading Requests End to End Integration Test happy path
     Failure/Error:
       expect{ ReadingRequests::Initiate.call(cat: kitty, request_data: request_data) }
           .to change{ kitty.book_requests.count }
           .by(1) # initiating a reading request should only create one book request for cat

       expected `kitty.book_requests.count` to have changed by 1, but was changed by 0
     # ./spec/lib/reading_requests/integration_spec.rb:19:in `block (2 levels) in <top (required)>'

Book requests aren't being created for our kitties. Is this a bug in my test set up or code?

  • First, let's try to reload the kitty when we're expecting change. Hmmm... still failing.

Why is our test failing but our method isn't raising an error?

  • Let's pop a binding.pry in our test and in our action - commit w/ prys
  • What does cat.book_requests look like in the test? In our class? Inspect in console
  • Change create to create! in your main call method to make your failure louder

Commits with extra notes/narration:

Woo hoo we have a basic pass! Now to test the notification side effect of initiate!

Testing Side Effects

Now that we've got the first action, let's test the other side effects of our initiate action -- wrangler notification.

  let(:wrangler_1) { FactoryBot.create(:cat_reading_wrangler) }
  let(:wrangler_2) { FactoryBot.create(:cat_reading_wrangler) }

  it "happy path" do
    # Initiating a request creates a book request for the cat
    # Sends a notification to the available cat reading wranglers
    expect{ ReadingRequests::Initiate.call(cat: kitty, request_data: request_data) }
      .to change{ kitty.book_requests.count }
      .by(1) # initiating a reading request should only create one book request for cat
      .and change{ ActionMailer::Base.deliveries.count }
      .by(2) # the number of available wranglers
  end

Run tests... pass/fail?

Fail.

1) Reading Requests End to End Integration Test happy path
     Failure/Error:
       expect{ ReadingRequests::Initiate.call(cat: kitty, request_data: request_data) }
         .to change{ kitty.book_requests.count }
         .by(1) # initiating a reading request should only create one book request for cat
         .and change{ ActionMailer::Base.deliveries.count }
         .by(2) # the number of available wranglers

       expected `ActionMailer::Base.deliveries.count` to have changed by 2, but was changed by 0

Our mailer's not being sent? Did we set up the mailer correctly or is this our test set up? Let's pry around in there

Solution to get them passing:

results matching ""

    No results matching ""