Skip to content

Key-based Import doesn't get stubbed #19

Open
@mgpnd

Description

@mgpnd

Describe the bug

When a dependency is imported via key-based argument, stubbing doesn't affect it's resolving.

I've made a sample app that reproduces the issue: https://github.com/mgpnd/import_example

Here's the most important snippets from the app:

# Operation that will be imported and stubbed
module Example
  class Show
    def call
      self.class.name
    end
  end
end
class ExampleController < ApplicationController
  include ImportExample::Import[
    show_example: "example.show"
  ]

  def index
    @auto_imported = show_example.()
    @manually_resolved = ImportExample::Container["example.show"].()
  end
end

/spec/controllers/example_controler_spec.rb:

RSpec.describe ExampleController, type: :controller do
  describe '#index' do
    subject { get :index, params: { format: 'text/hmtl' } }

    ...
    
    before do
      # Stubbing is enabled somewhere else so this part works properly
      ImportExample::Container.stub('example.show', -> { 'operation stub' })
    end

    # Fails, @auto_imported == "Example::Show"
    it 'calls auto imported stub' do
      subject
      expect(assigns(:auto_imported)).to eq('operation stub')
    end

    # Passes, @manually_resolved == "operation stub"
    it 'calls manually resolved stub' do
      subject
      expect(assigns(:manually_resolved)).to eq('operation stub')
    end
  end
end

To Reproduce

  1. Define a dependency via auto_inject Import with key-based argument
  2. Stub the dependency in any test

Expected behavior

The dependency resolved to provided stub.

Your environment

  • Affects my production application: NO
  • Ruby version: 2.7

I've tried to reproduce the issue with plain dry-auto_inject and with basic dry-system, but it doesn't appear there, in both gems importing works properly so I'm reporting it here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions