title: Writing TestCase for CSV import rake task
body: <p>I have a simple rails application where I import data from csv into my rails app which is functioning properly, <strong>but I have no idea where to start with testing this rake task, as well as where in a modular rails app</strong>. Any help would be appreciated. Thanks!</p>
  
  <p><strong>Hint</strong></p>
  
  <blockquote>
    <p>My Rails structure is a little different from traditional rails structures, as I have written a Modular Rails App. My structure is in the picture below:</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/88Ohx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/88Ohx.png" alt="My Rails Structure"></a></p>
  
  <p><strong>engines/csv_importer/lib/tasks/web_import.rake</strong></p>
  
  <p>The rake task that imports from csv..</p>
  
  <pre><code>require 'open-uri'
  require 'csv'
  
  namespace :web_import do
    desc 'Import users from csv'
  
    task users: :environment do
      url = 'http://blablabla.com/content/people.csv'
      # I forced encoding so avoid UndefinedConversionError "\xC3" from ASCII-8BIT to UTF-8
      csv_string = open(url).read.force_encoding('UTF-8')
  
      counter = 0
      duplicate_counter = 0
  
      user = []
      CSV.parse(csv_string, headers: true, header_converters: :symbol) do |row|
        next unless row[:name].present? &amp;&amp; row[:email_address].present?
        user = CsvImporter::User.create row.to_h
        if user.persisted?
          counter += 1
        else
          duplicate_counter += 1
        end
      end
      p "Email duplicate record: #{user.email_address} - #{user.errors.full_messages.join(',')}" if user.errors.any?
  
      p "Imported #{counter} users, #{duplicate_counter} duplicate rows ain't added in total"
    end
  end
  </code></pre>
  
  <p><strong>Mounted csv_importer in my parent structure</strong> </p>
  
  <p>This makes the csv_importer engine available in the root of the application.</p>
  
  <pre><code>Rails.application.routes.draw do
    mount CsvImporter::Engine =&gt; '/', as: 'csv_importer'
  end
  </code></pre>
  
  <p><strong>To correctly migrate in the root of the application, I added initializer</strong></p>
  
  <p><strong>/engines/csv_importer/lib/csv_importer/engine.rb</strong></p>
  
  <pre><code>module CsvImporter
    class Engine &lt; ::Rails::Engine
      isolate_namespace CsvImporter
  
      # This enables me to be able to correctly migrate the database from the parent application.
      initializer :append_migrations do |app|
        unless app.root.to_s.match(root.to_s)
          config.paths['db/migrate'].expanded.each do |p|
            app.config.paths['db/migrate'] &lt;&lt; p
          end
        end
      end
    end
  end
  </code></pre>
  
  <p>So with this explanation am able to run rails app like every other rails application. I explained this so anyone who will help will understand what to help me with as regards writing test for the rake task inside the engine.</p>
  
  <p><strong>What I have done as regards writing TEST</strong></p>
  
  <pre><code>task import: [:environment] do
    desc 'Import CSV file'
    task test: :environment do
      # CSV.import 'people.csv'
      Rake::Task['app:test:db'].invoke
    end
  end
  </code></pre>
  
  <p>How do someone write test for a rake task in a modular app? Thanks!</p>