简体   繁体   中英

Three Rake tasks with highline stubs

I have been trying to use the following spec based on this Stack Overflow question to cover this process.

Current Spec...

describe 'rake task myapp:data:load:from' do
  include_context 'rake'
  let(:task_path) { 'tasks/data.rake' }

  describe ':range', focus: true do
    let(:created_before) { '2017-01-02' }
    let(:created_after) { '2017-01-01' }
    let(:task_name) { 'myapp:data:load:range' }
    before do
      allow(highline).to receive(:ask).and_return(true)
      # rubocop:disable all
      allow_any_instance_of(Object).to receive(:created_after).and_return(created_after)
      allow_any_instance_of(Object).to receive(:created_before).and_return(created_before)
      # rubocop:enable all
    end

    it 'processes from a specified starting point' do
      # expect(Resque).to receive(:enqueue).with(AmazonMws::ImportOrdersJob, created_from: created_from)
      invoke_task.invoke
    end
  end

  describe ':from' do
    let(:created_from) { '2017-01-01' }
    let(:task_name) { 'myapp:data:load:from' }
    before do
      allow(highline).to receive(:ask).and_return(created_from)
    end

    it 'processes from a specified starting point' do
      expect(Resque).to receive(:enqueue).with(AmazonMws::ImportOrdersJob, created_from: created_from)
      invoke_task.invoke
    end
  end
end

The rake task...

namespace :myapp do
  namespace :data do
    namespace :load do
      desc 'Enqueue a range of dates by provider'
      task range: :environment do
        cli = HighLine.new
        msg('Select the dates for your data pull. Format: yyyy-mm-dd')
        created_before = cli.ask('Data created_before? ', Date)
        created_after = cli.ask('Data created_after? ', Date)

        if created_after >= created_before
          error_msg('The created_after date must be less than created_before!')
          exit
        else
          Resque.enqueue(AmazonMws::ImportOrdersJob, created_before: created_before, created_after: created_after)
        end
      end

      desc 'Enqueue from a specified date by provider'
      task from: :environment do
        cli = HighLine.new
        msg('Select the date for your data pull. Format: yyyy-mm-dd')
        created_from = cli.ask('Data created_from? ', Date)
        Resque.enqueue(AmazonMws::ImportOrdersJob, created_from: created_from)
      end
    end
  end
end

spec_helper.rb

require 'rubygems'

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)

# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?

require 'rspec/rails'
require 'require_all'
require 'shoulda/matchers'
require 'pry'
require 'webmock/rspec'
require 'vcr'
require 'json_matchers/rspec'

# Only Allow LocalHost Tests!
WebMock.disable_net_connect!(allow_localhost: true)

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join('spec/concerns/**/*.rb')].each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

# Model Specs          => type: :model
# Controller Specs     => type: :controller
# Request Specs        => type: :request
# Feature Specs        => type: :feature
# Service Specs        => type: :service
# View Specs           => type: :view
# Helper Specs         => type: :helper
# Mailer Specs         => type: :mailer
# Routing Specs        => type: :routing
# Constraint Specs     => type: :constraint

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.order = 'random'
  config.seed = srand % 0xFFFF
  config.infer_spec_type_from_file_location!
  config.raise_errors_for_deprecations!
  config.use_transactional_fixtures = false
  config.infer_base_class_for_anonymous_controllers = false

  config.filter_run focus: true
  config.run_all_when_everything_filtered = true

  config.before(:each) { GC.disable }
  config.after(:each) { GC.enable }
  config.include JsonSpec::Helpers
  config.include LoadFixtureHelper

  config.before(:all) do
    Rails.cache.clear
  end
end

How do I stub out the two separate Ask calls? Right now, after the misspelling I have the other rake task that has a single call working correctly.

To stub the two different calls from the range task, you can pass with to your allows, like this:

allow(highline).to receive(:call).with('Data created_before? ', Date).and_return(true)
allow(highline).to receive(:call).with('Data created_after? ', Date).and_return(false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM