Skip to content

Instantly share code, notes, and snippets.

@jonesdeini
Last active February 25, 2026 16:07
Show Gist options
  • Select an option

  • Save jonesdeini/d214d9b111651bbf41b771c8ee3eb259 to your computer and use it in GitHub Desktop.

Select an option

Save jonesdeini/d214d9b111651bbf41b771c8ee3eb259 to your computer and use it in GitHub Desktop.
Conversations spec stubs incorrect empty response shape

Conversations#list spec stubs an impossible response shape

The issue

In spec/services/canvas/client/conversations_spec.rb, the "when API returns empty array" test stubs the /api/v1/conversations endpoint to return []:

context 'when API returns empty array' do
  it 'returns empty array' do
    stubs.get('/api/v1/conversations') do
      [200, {}, []]
    end
    # ...
  end
end

But Conversations#list always passes include_all_conversation_ids: true, which changes the Canvas response format from an array to a Hash:

response = http_client.get(
  ENDPOINT,
  include_all_conversation_ids: true,
  **params,
)

What Canvas actually returns

Canvas's conversations_controller.rb wraps the response when include_all_conversation_ids is set:

if params[:include_all_conversation_ids]
  @conversations_json = {
    conversations: @conversations_json,
    conversation_ids: @conversations_scope.conversation_ids
  }
end

So the real empty response is:

{ "conversations": [], "conversation_ids": [] }

Not [].

Our own shared context (spec/support/shared_examples/service_responses/canvas_conversations.rb) correctly uses a Hash for the successful case — the empty case should match.

Fix

Change the stub to return the actual response shape:

context 'when API returns empty conversations' do
  it 'returns empty array' do
    stubs.get('/api/v1/conversations') do
      [200, {}, { 'conversations' => [], 'conversation_ids' => [] }]
    end

    result = conversations_client.list(scope:)

    expect(result).to eq []
  end
end

The individual conversation stubs in that test can also be removed since conversation_ids is empty — no individual fetches will be made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment