These are chat archives for dry-rb/chat

20th
Feb 2018
Jonah
@jonahx
Feb 20 2018 05:16

Consider this snippet from the auto_inject docs:

Import = Dry::AutoInject(MyContainer)

class CreateUser
  include Import["users_repository"]

  def call(user_attrs)
    users_repository.create(user_attrs)
  end
end

How would I swap out a container full of mock objects in a testing environment? I’m having trouble deciding the best way to organize things… i assume MyContainer is a global module you’d include… but if I’m doing something like require ‘my_container’ at the top of create_user.rb, it’s not clear how I’d swap that out?

Tim Riley
@timriley
Feb 20 2018 05:21
Are you using dry-system, @jonahx?
Jonah
@jonahx
Feb 20 2018 05:23
@timriley no, not yet
is that the correct answer to my question?
Tim Riley
@timriley
Feb 20 2018 05:25
Well, it does some things to help with testing, that’s for sure
you’re looking to unit test a single class?
Anyway, dry-container does come with stubbing support, for testing: http://dry-rb.org/gems/dry-container/testing/
But I would go another way, I’d simply pass in test double when initializing the object you’re testing
subject(:create_user) { CreateUser.new(users_repo: users_repo) }
let(:users_repo) { double(:repo, …) }
Jonah
@jonahx
Feb 20 2018 05:30
@timriley, ah, so you can always override by passing in directly, and if you leave the arguments out it pulls them from what’s specified in Import?
Tim Riley
@timriley
Feb 20 2018 05:31
Yep
This is one of the nice things of the whole arrangement
(and if you’re using dry-system you can leave your container unfinalized and it’ll lazily require/load only the files for those dependencies it has to resolve)
Jonah
@jonahx
Feb 20 2018 05:32
@timriley yep that’s exactly how it should work. i was trying to work around it not working like that, which i just assumed… should have known better.
Jonah
@jonahx
Feb 20 2018 06:22
I notice in the below that setting up an auto-register dir is a separate step from updating the $LOAD_PATH with load_path!. In what circumstance would you do the auto-register step without also updating the load path?
class Application < Dry::System::Container
  configure do |config|
    config.root = Pathname('./my/app')

    # we set 'lib' relative to `root` as a path which contains class definitions
    # that can be auto-registered
    config.auto_register = 'lib'
  end

  # this alters $LOAD_PATH hence the `!`
  load_paths!('lib')
end
Piotr Solnica
@solnic
Feb 20 2018 07:23
@jonahx when lib is already in your load path
Taskhyn Maksim
@sarbazx
Feb 20 2018 11:47
Hi all, is there any good tutorial for web development using dry-rb gems?
Gustavo Caso
@GustavoCaso
Feb 20 2018 11:50
@sarbazx This guide is pretty complete http://dry-web-roda-todo-app.readthedocs.io/en/0.9.1/
Is written by @alejandrobabio
Taskhyn Maksim
@sarbazx
Feb 20 2018 12:02
@GustavoCaso thanks!
Gustavo Caso
@GustavoCaso
Feb 20 2018 23:49
Thanks @alejandrobabio helping with this release :clap: