0

First off, I am using the Interactor Gem. (You should check it out if you haven't. Pretty cool lib)

Let's say I've got an Interactor::Organizer as follows:

class InitiateImport
  include Interactor::Organizer

  organize CreateImport, QueueImport
end

And I've also got two Interactors as follows:

class CreateImport
  include Interactor

  def call
    import = Import.create()
    if import
      context.import = import
    else
       context.fail!(message: 'import failed!')
    end
  end
end

and

class QueueImport
  include Interactor

  def call
    unless QueueHandler.perform_later(context.import.id)
      context.fail!(message: 'Queue broke!')
    end
  end
end

BIG QUESTION - How do I -- using Minitest -- test that organize is being called on InitiateImport with the argument [CreateImport, QueueImport]

assert_send will let me verify that organize is being called, but it won't let me verify what arguments I pass it. Something like this:

class InitiateImportTest < Minitest::Test  
  def test_organize_called
    InitiateImport.new(file: file)

    assert_send([InitiateImport, :organize, [CreateImport, QueueImport]])
  end
end

I found this question / answer from nearly 3 years ago, but it didn't provide much clarity for me. Maybe it's in there and I'm just missing something?

I also realize that I could just call the organizer with valid param data such that it passes and I verify the output from there, however I've already got those two classes under test and I'd basically just be exercising the same functionality twice. I'd really like to just verify method invocation and arguments if possible.

Any help or suggestions would be greatly appreciated.

Community
  • 1
  • 1
jamesconant
  • 1,405
  • 1
  • 12
  • 18
  • Hey! I think `spy` might help you achieving what you're looking for, check my other [answer](http://stackoverflow.com/questions/28813062/idiomatically-mock-openuri-open-uri-with-minitest/28878374#28878374). Hope that helps! – Paweł Dawczak Apr 02 '15 at 12:54
  • Thanks! I will look into that and report back. – jamesconant Apr 02 '15 at 12:57
  • Yeah... It's quite long thou... The things you should focus on, are marked with numbers `(2)` and `(3)` in first code listing. – Paweł Dawczak Apr 02 '15 at 13:00
  • It dawns on me that maybe I should just setup the main test expectations in the organizer test, and not worry about testing the two interactors as those are just 'implementation details' – jamesconant Apr 04 '15 at 17:10

1 Answers1

1

You cannot test the params because the params is being set on the test. A test have to test the app code, not the test itself.

You should test if the Organizer is doing what it is intended to do.

In your case, you are queuing a background job, you can test if the queue is being changed, for exemple using Sidekiq:

InitiateImport.call(params: params)

assert_equal 0, QueueHandler.jobs.size
QueueHandler.perform_async(1, 2)
assert_equal 1, QueueHandler.jobs.size
gamendola
  • 134
  • 4