# Using a double with Instance attribute modifier

**URL:** <https://discourse.dry-rb.org/t/using-a-double-with-instance-attribute-modifier/913>\
**Category:** Uncategorized\
**Created:** [October 3, 2019, 12:16pm UTC](https://discourse.dry-rb.org/t/using-a-double-with-instance-attribute-modifier/913 "2019-10-03T12:16:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fotanus](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/fotanus/32/179_2.png) [@fotanus](https://discourse.dry-rb.org/u/fotanus)\
**Post date:** [October 3, 2019, 12:16pm UTC](https://discourse.dry-rb.org/t/using-a-double-with-instance-attribute-modifier/913/1 "2019-10-03T12:16:19Z")

</div>

Hello,

Is it possible for a double to bypass the `Instance` check on a dry-struct attribute? For instance:

```ruby
require 'dry-struct'
require 'spec_helper'

class B
end

class A < ::Dry::Struct
  include Dry::Types.module
  constructor_type(:schema)
  attribute :b, Instance(B).optional
end

describe 'something' do
  it 'test' do
    b = instance_double('B')
    expect(
      A.new(b: b)
    ).to be
  end
end

```

I’ve tried to mock several methods from both `B` and `b` and was not able to make this test pass.

---

<div class="post-metadata">

**Author:** ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)\
**Post date:** [October 5, 2019, 7:08am UTC](https://discourse.dry-rb.org/t/using-a-double-with-instance-attribute-modifier/913/2 "2019-10-05T07:08:44Z")

</div>

`instance_double` returns something that is not an instance of `B`, so the check in `Instance(B)` will fail whatever you do. Maybe you can find another way to design and test your behavior without the need of doubles, probably allowing your dependencies to be injected so that you can use lighter ones in your test suite:

```ruby
# Without injection

class A
  def foo
    # You won't be able to change B in your test suite as it is a hard coded dependency.
    B.new.do_something
  end
end

```

```ruby
# With injection

class A
  attr_reader :b
  
  def initialize(b:)
    @b = b
  end

  def foo
    # Now, in your test suite you can do `A.new(b: TestB.new)` and you don't need to create a double for B
    b.do_something
  end
end

```

[dry-auto\_inject](https://dry-rb.org/gems/dry-auto_inject/0.6/) can help you to avoid boilerplate code.

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [October 5, 2019, 10:35am UTC](https://discourse.dry-rb.org/t/using-a-double-with-instance-attribute-modifier/913/3 "2019-10-05T10:35:56Z")

</div>

Storing something beyond plain data structures in dry-struct seems like an anti-pattern to me.
