title: Should I test the command pattern with pure unit tests or integration tests?
body: <p>The command pattern is a pretty good way of avoiding fat controllers and fat models where several components have to work together. This is the place where business logic is implemented.
  The question is that I'm not sure if 'pure' unit testing has sense here or it is better to write integration tests instead. As an example:</p>
  
  <pre><code>class MyCommand
    def initialize(attributes)
      @attributes = attributes
    end
  
    def execute
      objA = ClassA.find(@attributes[:class_a_id])
      objB = ClassB.find(@attributes[:class_b_id])
  
      objA.do_something
      objB.do_something_with_a(objA)
  
      objA.save
      objB.save
    end
  end
  </code></pre>
  
  <p>Note that this is an example. Of course we could write a much better command, but this one is fine as an example.</p>
  
  <p>As we can see, objA and objB are instantiated inside the command. I could use DI to avoid this internal dependency, but if I do this, the instantiation would be done inside the controller which uses this command and this is exactly what I don't want.</p>
  
  <p>If I write an unit test for this, I would have to stub ClassA.find and ClassB.find methods and check that do_something and do_something_with_a is called. Also we have to check that save is called as well. Too much stubbing over here and a very brittle test.</p>
  
  <p>We could solve a piece of this by using FactoryGirl</p>
  
  <pre><code>...
  before do
    FactoryGirl.create(:class_a)
    FactoryGirl.create(:class_b)
  end
  ...
  </code></pre>
  
  <p>And then, we don't need to stub ActiveRecord find methods. </p>
  
  <p>To avoid expected method invocation on save, we could check that the database has been updated  properly.</p>
  
  <p>If we chose this second approach, we are writing an integration test that involves collaboration among ClassA, ClassB, MyCommand and the database. Actually this is like a functional test because we are testing a piece of the functional logic (just like we do with controller but framework independent).</p>
  
  <p>So, the question is, is it worth it to write pure unit tests for commands?</p>
  
  <p>Thanks in advance</p>