当我发现种子文件有问题时,我正在用 Faker 的几千条记录为开发 postgres 数据库做种。我中止了种子操作并回滚了插入并修复了seeds.rb 文件。
当我再次运行它时,每个rake db:*任务都运行了两次。我可以跑得rake routes很好,但如果我跑了,rake db:drop我会得到这样的结果:
$ rake db:drop
Dropped database 'vp_development'
Dropped database 'vp_development'
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行 migrate ,当它尝试应用索引时,整个事情就会崩溃,因为它已经创建了这些列。
我只有一个默认的 Rakefile,在 lib 或其他任何地方都没有自定义 rake 文件。
如果有任何区别,环境是 rails 5.0.0.1 和 ruby 2.2.2。我现在很迷茫。
这是我的 Rakefile
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative 'config/application'
Rails.application.load_tas
Run Code Online (Sandbox Code Playgroud)
我已经看到其他线程暗示它可能是 gem 的问题,但是在这个问题开始的几天内我没有添加新的 gem。无论如何,这是gemfile。
source 'https://rubygems.org'
# Bundle edge Rails …Run Code Online (Sandbox Code Playgroud) 我有一个rake任务测试,我按照我在网上找到的唯一例子进行设置.
它看起来像这样:
require 'test_helper'
require 'minitest/mock'
require 'rake'
class TestScrapeWelcome < ActiveSupport::TestCase
def setup
Rake.application.init
Rake.application.load_rakefile
@task = Rake::Task['scrape:scrape']
@task.reenable
end
def teardown
Rake::Task.clear
end
test "scraping text and sending to elasticsearch" do
mocked_client = Minitest::Mock.new
get_fixtures.each_with_index do |arg,i|
mocked_client.expect :index, :return_value, [index: "test", type: 'welcome', id: i, body: arg]
end
Elasticsearch::Model.stub :client, mocked_client do
@task.invoke
end
assert mocked_client.verify
end
private
def get_fixtures
(0..11).map { |i|
File.read("test/fixtures/scrape/index_#{i}.json")
}
end
end
Run Code Online (Sandbox Code Playgroud)
但是一旦任务开始运行,它再次开始运行而没有我做任何事情(puts在@task.invoke显示之前和之后打印任务只运行一次).