使用高线存根的三个Rake任务

时间:2017-01-29 02:48:45

标签: ruby-on-rails rspec highline

我一直在尝试使用基于this Stack Overflow question的以下规范来涵盖此过程。

当前规范......

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

佣金任务......

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

如何固定两个单独的Ask电话?现在,在拼写错误之后,我有另一个rake任务,只有一个呼叫正常工作。

1 个答案:

答案 0 :(得分:0)

要隐藏range任务中的两个不同的来电,您可以将with传递给您的允许,如下所示:

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)