database_cleaner 正在擦除我的开发数据库

2024-03-30

I have database-cleaner为我的 Rails 4 应用程序配置, 每次运行测试时,我发现我的数据库在两个版本中都被清除了test and development环境。

我的配置在rails_helper如下:

ENV["RAILS_ENV"] ||= 'test'
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'database_cleaner'
Rails.env = "test"
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# 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!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

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

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


  config.mock_with :rspec

  config.before(:all) do
    ActiveRecord::Base.skip_callbacks = true
  end

  config.after(:all) do
    ActiveRecord::Base.skip_callbacks = false
  end

end

我怎样才能确保清洁器只擦除数据库test environment不碰我的development?

My database.yml如下:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: directory-service_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: directory-service

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: directory-service_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: directory-service_production
  username: directory-service
  password: <%= ENV['DIRECTORY-SERVICE_DATABASE_PASSWORD'] %>

我建议改变

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

to

ENV["RAILS_ENV"] = 'test'

并删除

Rails.env = 'test'

因为 RAILS_ENV 环境变量应该足以进行配置

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

database_cleaner 正在擦除我的开发数据库 的相关文章

  • 在 Rails 中,我可以通过委托方法订购查询吗?

    我在通过委托方法订购查询时遇到困难 我的任务是帮助将一个相当大的 Rails 3 应用程序升级到 Rails 4 我在索引操作中遇到了这个查询 我知道这些对象的命名是可怕且令人困惑的 measurements controller rb d
  • 使用 RSpec 进行 Rails 片段缓存测试

    我觉得这是一个没有太多记录的主题 至少我在这里找到最佳实践时遇到了很多麻烦 我使用 cache key 在视图中进行片段缓存 tbody employees each do employee cache employee do tr emp
  • 如果不选择 Updated_at 属性,则无法更新 Rails 4 中的模型

    我正在从 Rails 3 2 13 升级到 4 并遇到更新模型的问题 我正在尝试仅更新模型上的单个属性 其中我只有 id 我还需要验证和回调 这在 Rails 3 中运行良好 但在 Rails 4 中遇到了麻烦 在 Rails 4 中 如果
  • Capybara 无法从 Stripe 中找到表单字段?

    我正在学习 Ruby on Rails 并且正在开发一个使用 stripe 创建高级帐户的应用程序 另外 我正在使用 Rspec 和 Capybara 进行集成测试 require spec helper feature user upgr
  • render_views 在 rspec 中做什么?

    我对 rspec 很陌生 对 Rails 也很陌生 在为控制器编写测试用例时 我注意到render views经常使用 这是做什么的 如果我不调用它会发生什么 它呈现控制器规范中的视图 如果不放置 render views 视图将不会渲染
  • 表单提交按钮仅在重新加载后才有效

    我有一个构建表的索引页 我试图允许用户编辑表中的行 我试图以最基本的方式做到这一点 没有 javascript ajax 等 除非 Rails 提供了它 我的表在索引方法中显示良好 并且有一个表单作为表中的最后一行 可用于添加新行 新的形式
  • Rspec:测试救援

    尝试测试我的函数是否能够正确地从异常中解救 更改参数 文件名 然后重试一次 我可以让函数接收第一次尝试 但无法让它接收第二次尝试 控制器 begin video get video video id rescue matches video
  • 使用 Rails 4(和 Hstore)在 PostgreSQL 中存储嵌套哈希

    我有一个 Rails 应用程序 它聚合了来自 Google API 的大量数据 我当前将 JSON 响应存储在 MongoDB 中 因此我的 Rails 应用程序同时具有 pg 和 mongo 然而 今天 我遇到了 PostgreSQL H
  • 使用 RSpec 测试控制器时模拟 CanCan 授权

    这是我要测试的控制器 class UsersController lt ApplicationController load and authorize resource def index users User all respond t
  • 将局部变量向下传递 Rails 4 中的部分层次结构以实现 Ransack 关联

    我正在尝试使用 Ransack 在我的模型的所有索引页上实现搜索和排序表单 我希望尽可能地做到这一点 因此我在视图 应用程序文件夹中创建了三个部分 视图 application table search and sort form html
  • 使用 RSpec 测试 rake 任务后清除数据库

    我正在尝试使用 rspec 测试我的 rake 任务 它运行良好 但问题是记录随后不会被删除 我已经把config use transactional fixtures true在配置文件中没有影响 在其他测试中它运行良好 这是我的代码 r
  • Rspec——需要存根在另一个文件中调用的 File.open

    在我的测试中 我正在初始化一个名为的新类Package带有一些参数 在此类的初始化中 我打开一个在远程机器上可用的文件 但不是本地常见的文件 我想知道如何在测试中对该方法进行存根 我正在使用 rspec 和 mocha 我尝试过类似的东西
  • rspec 在需要存根的私有方法中测试私有方法

    Simplecov 检测到我遗漏了一些测试lib api verson rb class class ApiVersion def initialize version version version end def matches req
  • 如何检查水豚中的复选框?

    我正在使用 Rspec 和水豚 我怎样才能写一个步骤来检查checkbox 我试过了check按价值但它找不到我的checkbox 我不知道该怎么做 因为我实际上有相同的 ID 但值不同 这是代码
  • Capybara 的 has_selector 有哪些选项?

    我在 RSpec 中遇到此错误 有没有任何文档have selector解释了选项哈希中的每个键以及它到底有什么作用 invalid keys content should be one of text visible between co
  • Rspec to have(n).items undefined 方法

    我正在尝试遵循指南关于代码 tuts http code tutsplus com tutorials ruby for newbies testing with rspec net 21297我不断收到错误 这是我的库规范 require
  • rspec 中的模拟方法链

    有一系列方法可以获得user目的 我试图模拟以下内容以返回user in my Factory Girl current user AuthorizeApiRequest call request headers result 我可以模拟该
  • 在 Rails 中呈现路由错误的 404 页面

    我试图在 Rails 中渲染集成的 404 页面作为例外 我尝试了这个 但仍然收到路由错误页面 posts controller rb def destroy if current user username post email post
  • 如何使用 RSpec 3.4 在视图规范中存根控制器辅助方法?

    我正在测试一个视图RSpec 3 4 上Rails4 2 5 我用的是体面的曝光我的 CRUD 控制器中的 gem 体面的曝光可以轻松定义可用于我的视图并记住结果值的命名方法 但我不明白如何在视图规范中存根这些方法 我尝试根据 RSpec
  • RSpec 2 中使用 OR 相等

    下面这个例子的正确写法是什么 玩家的分数应等于 5 或 8 it should equal 5 or 8 do player score should 5 or 8 end Thanks Tim 5 or 8将始终产生结果 5 并且不会执行

随机推荐

  • 下载 ASP.NET MVC C# 中字节数组列表中包含的多个文件

    我正在开发一个 ASP NET MVC 5 应用程序 我编写了一个代码 允许我下载存储在 SQL Server 数据库中的文件 varbinary 我可以使用以下命令下载单个文件 public JsonResult PrepareSingl
  • 如何将公历转换为中国农历?

    我想使用公历到中国农历构建一个Android应用程序 我不知道如何从公历转换为农历 我怎样才能做到这一点 从公历到中文的转换 我刚刚发布了Time4J的新版本 v4 35 但是使用Time4A https github com MenoDa
  • 在caffe中定义新层时如何获取学习率或迭代次数

    我想当迭代次数达到一定次数时改变损失层中的损失计算方法 为了实现它 我认为我需要获取当前的学习率或迭代次数 然后我使用if短语选择是否改变损失计算方法 您可以添加一个成员变量咖啡类保存当前的学习率或迭代次数并在您想要的层中访问它 例如 要获
  • 如何从命令行删除钥匙串引用

    From the Keychain app I have the option to delete just the reference to a listed Keychain that was unlocked and listed v
  • 使用元类覆盖复杂内置方法

    作为学习练习 我正在尝试实现一个类来模拟 python 的行为complex内置的 但具有不同的行为 str and repr 方法 我希望它们以以下格式打印 1 0 2 0 代替 1 2j 我首先尝试简单地从complex并重新定义 st
  • 在 Fortran 语言中 advance='no' 是什么意思?

    我是 Fortran 新手 我不明白这一行 write a35 advance no in program democonvertion implicit none real tc tf tr tk write a35 advance no
  • Windows RT .NET 兼容性

    我只需要知道我是否需要相同的框架来开发适用于平板电脑 Windows RT 和 PC 的 Windows 8 应用程序 或者平板电脑仍然使用 NET 紧凑框架 在 Windows RT 即 Windows 8 的平板电脑 ARM 版本 上运
  • C# 中的字节数组加密

    我想使用按位运算符创建一个很好的密码学 然而我没有这样做 我希望它具有使用字节数组的按位运算符来加密和解密我的字节数组 public class Cryptographer private byte Keys get set public
  • 从 F# 访问名称中包含 .base 的命名空间

    正如标题所示 我试图使用在名称中包含 base 的名称空间中声明的类 考虑如下情况 open Foo base Bar 在 C 中 我只是在基数之前使用 但 F 似乎忽略了这一点 并认为 是用于列表连接的中缀运算符 由于命名空间属于我无法修
  • 创建每个键包含多个值的字典

    如何创建一个字典 其中每个键包含 2 个列表中的多个值 例如 我有 gt gt gt list1 fruit fruit vegetable gt gt gt list2 apple banana carrot 而且 我想要一些效果 gt
  • 将 jquery masonry 集成到 zurb 基础网格中

    我在将砌体与基础网格布局集成时遇到问题 本质上 对于具有不同高度的相同宽度图像 砌体按预期工作 但在某些断点处 网格将仅具有两列布局 而不是通常的四列布局 但是 如果您继续最小化浏览器宽度 则会返回四列 因此不可能没有空间来显示它们 var
  • 通过父id和mysql中的where子句获取所有子项

    我有一个表 将 id 和parent id 存储在同一个表中 我想要一个接受parent id 作为参数并返回第n 级的所有子节点的递归查询 为此 我正在使用此代码并为我正常工作 select id name parent from sel
  • 如何使用 PowerShell 发送电子邮件

    我想从 PowerShell 发送电子邮件 所以我使用这个命令 EmailFrom email protected cdn cgi l email protection EmailTo email protected cdn cgi l e
  • 使用 eval 和 eval-source-map 有什么区别?

    我正在使用 webpack 来配置源映射 我想知道有人可以澄清 eval 和 eval source map 之间的区别吗 我个人看不出有什么区别 来自文档 https webpack js org configuration devtoo
  • .NET Core 的 IWebProxy 实现的位置

    System Net IWebProxy 的可用实现是什么 来自 System Net Primitives DNX Core 根据应用程序要求 dnxcore50 中可以使用唯一的框架 那么包含代理实现的正确 NuGet 包是什么 解决此
  • 在运行时测试 iOS 版本特定功能的推荐方法

    我的目标是 IOS 4 3 和 5 0 其中一个应用程序是针对 5 0 SDK 构建的 并且希望仅当该应用程序在 iOS5 设备上运行时才添加对 iOS5 中引入的 Twitter 功能的支持 在运行时可靠地测试这些操作系统功能的可用性而不
  • Apollo GraphQL 请求被取消

    我正在使用 React 类型前置文本字段组件Downshift and react apollo 当用户输入时 我正在查询输入建议并将其显示在文本字段下方 不幸的是 这段经历并不顺利 由于某种原因 Apollo 时不时会取消 50 以上的请
  • 使用 facebook graph api 搜索好友

    Facebook 是否有用于搜索用户好友的 API Tim 您可以这样搜索朋友的名字 select uid name sex from user where uid in SELECT uid2 FROM friend WHERE uid1
  • 对于阿拉伯语应用程序,英文字母的顺序相反(在 wkwebview 内)

    您好 当我将操作系统更新到版本 14 时 我在 webview 中看到了一个问题 即我的阿拉伯应用程序上的英文字母顺序相反 我的应用程序本地化设置为 Localization native development region ar 并使用
  • database_cleaner 正在擦除我的开发数据库

    I have database cleaner为我的 Rails 4 应用程序配置 每次运行测试时 我发现我的数据库在两个版本中都被清除了test and development环境 我的配置在rails helper如下 ENV RAIL