【问题标题】:Three Rake tasks with highline stubs三个带有 highline 存根的 Rake 任务
【发布时间】:2017-01-29 02:48:45
【问题描述】:

我一直在尝试使用以下基于 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

rake 任务...

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 任务,它有一个调用正常工作。

【问题讨论】:

  • 存根失败了怎么办?你能包括你得到的错误吗?
  • @oolong 我刚刚添加了这个,很抱歉留下了这个。
  • 您使用的是 rspec 3 吗?您是否尝试在规范文件顶部包含 rails_helper
  • @oolong 是的。我刚刚用规范助手更新了这个问题。它已与 rails 助手合并。
  • 你只是有错字吗?看起来你的错误是 HighlLine 而不是 HighLine,而且你的规范也是存根 HighlLine

标签: ruby-on-rails rspec highline


【解决方案1】:

要存根来自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)

【讨论】:

    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 2020-12-24
    • 2010-12-25
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多