【问题标题】:Can't clear Mongo DB while running test运行测试时无法清除 Mongo DB
【发布时间】:2015-06-11 13:22:47
【问题描述】:

我正在使用railsmongoidsporkrspec

在通过rspec 运行测试时,我发现我的数据库中的记录数量不断增加。 purge!database_cleaner 都没有帮助。

我的测试是:

describe MyConvertor do
  context 'working with my model'
    before(:each) do
      FactoryGirl.create :my_model
    end
    # examples go here
  end
end

我的规范助手是:

Spork.each_run do
  RSpec.configure do |config|
    # ...
    config.before(:each) do
      Mongoid.purge!
    end
    # ...
  end
end

正如我之前提到的,我也尝试过database_cleaner,但情况并没有改变:

Spork.prefork do
  RSpec.configure do |config|
    config.order = "random"
    config.use_transactional_fixtures = false
  end
end

Spork.each_run do
  RSpec.configure do |config|
    config.before(:suite) do
      DatabaseCleaner[:mongoid].strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

所以我马上有几个问题:为什么purge! 什么都不做,为什么DatabaseCleaner 不起作用。

我找到了a database cleaner issue,但没有任何有用的解决方案。

我正在使用

rails 3.2.11
mongoid 3.0.23

【问题讨论】:

    标签: ruby-on-rails mongodb rspec mongoid database-cleaner


    【解决方案1】:

    我在 Mac OSX el capitain 上有这个:

    在终端上:

    brew doctor
    

    它向我显示了这个警告:

    "Warning: /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew."
    

    然后我需要更改 /etc/paths 中的顺序,使“usr/local/bin”和“/usr/local/sbin”在最前面。

    再次在终端上:

    sudo vi /etc/paths
    
    /usr/local/bin
    /usr/local/sbin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    

    这解决了我遇到的问题。

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 2017-12-10
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多