- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
救世主。
在每个 RSpec 示例之后,我在清理数据库时遇到了问题。问题是,当我运行 rspec
命令时,users_controller_spec.rb 提示记录比示例预期的多。事实上,如果我使用 rails c
进行检查,就会按照它所说的那样创建记录。当我单独运行这个套件时,它会成功,所以我认为这是因为 DatabaseCleaner 没有清除其他规范创建的用户记录(用户记录的数量与 users_controller_spec 示例声称的额外记录相匹配)。它们是在 before :all
block 中创建的(如果重要的话)。
这是我的rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
require 'admin/v1/dashboard_controller'
# Requires supporting ruby files with custom matchers and macros, etc, in
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"
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerMacros, type: :controller
# 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 = true
config.include FactoryGirl::Syntax::Methods
config.infer_spec_type_from_file_location!
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
users_controller.rb
describe 'GET #index' do
it 'populates an array of users' do
user1 = create(:user)
user2 = create(:user)
get :index
expect(assigns(:users)).to match_array([user1, user2])
end
it 'renders :index template' do
get :index, {}
expect(response).to render_template :index
end
end
更新 1:这是创建额外的 user
记录的地方
require 'rails_helper'
describe Admin::V1::MessagesController do
let(:admin_user) do
admin_user = double('admin_user')
allow(request.env['warden']).to receive(:authenticate!).and_return(admin_user)
allow(controller).to receive(:current_admin_v1_admin_user).and_return(admin_user)
p '==='
end
before { login_admin_user admin_user }
describe 'GET #index' do
it 'renders :index template' do
get :index, {}
expect(response).to render_template :index
end
end
describe 'GET #get_users' do
before :all do
@user1 = create(:user, nickname: 'hiro')
@user2 = create(:user, nickname: 'elise')
end
context 'with params' do
it 'populates an array of users matching on nickname' do
get :get_users, format: :json, query: 'h'
expect(assigns(:users)).to match_array([@user1])
end
end
context 'without params' do
it 'populates an array of all users' do
get :get_users, format: :json
expect(assigns(:users)).to match_array([@user1, @user2])
end
end
end
describe 'GET #get_messages' do
before :all do
@user1 = create(:user)
@user2 = create(:user)
@message1 = create(:message, user_id: @user1.id)
@message2 = create(:message, user_id: @user1.id)
@message3 = create(:message, user_id: @user2.id)
end
context 'with user_id' do
it 'populates an array of messages with the user_id' do
get :get_messages, format: :json, user_id: @user1.id
expect(assigns(:messages)).to match_array([@message1, @message2])
end
end
end
end
最佳答案
不幸的是,RSpec 的 before(:all)
不能很好地处理事务测试。 before(:all)
中的代码在事务打开之前运行,这意味着在事务中止时不会回滚在那里创建的任何记录。您有责任在 after(:all)
中手动清理这些项目。
参见 rspec-rails#496和 Using before(:all) in RSpec will cause you lots of trouble unless you know what you are doing
after(:all) do
# before/after(:all) is not transactional; see https://www.relishapp.com/rspec/rspec-rails/docs/transactions
DatabaseCleaner.clean_with(:truncation)
end
关于ruby-on-rails - DatabaseCleaner 似乎不会在诉讼之间清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972969/
救世主。 在每个 RSpec 示例之后,我在清理数据库时遇到了问题。问题是,当我运行 rspec 命令时,users_controller_spec.rb 提示记录比示例预期的多。事实上,如果我使用
如何标记示例组,以便在每个示例之间不清理数据库,而是在整个组之前和之后清理数据库?未标记的规范应该清理每个示例之间的数据库。 我想写: describe 'my_dirty_group', :dont
我在 MySQL 上运行的 Rails 站点上使用 Capybara 和 Poltergeist 运行自动化测试。 我看到它在运行这个自动化套件时提交“数据”插入的位置,但 DatabaseClean
测试/test_helper.rb : ENV["RAILS_ENV"] ||= "test" require File.expand_path('../../config/environment',
我包括database_cleaner我的 Rails 应用程序中的 gem 。遵循 git repo 中给出的示例,并在 spec_helper 中包含以下代码: 方法一 config.befo
我正在尝试让 DatabaseCleaner 在我使用 Sequel 的非 Rails 应用程序上运行,但我遇到了各种各样的问题。看起来数据库没有正确设置 - RSpec.configure do |
我对 RSpec 一直坚持使用基于 xUnit 的测试框架有点迷茫,但我正在试一试。 规范编写方式的嵌套性质让我有些头疼,因为我应该在哪里进行数据库设置/拆卸。 根据 DatabaseCleaner自
我目前正在使用 Cucumber 和 databasecleaner 来测试我的应用程序。目前在我的 env.rb 中,我将清洁器设置为: DatabaseCleaner.strategy = :tr
我有几个包含参数和其他内容的表格,它永远不会改变! 但我不能告诉 database_cleaner 保留这些数据,每次测试后它都会截断我的所有数据库。 gem gem "cucumber-rails"
我看到这个很酷的方法只使用数据库清理器:使用 :js => true 截断 capybara 测试 在 spec_helper.rb 中: config.before(:each) do Data
我有几个数据库表的数据仅用于显示目的,它永远不会改变。加载所有种子数据需要很长时间。我正在使用 cucumber ,它似乎在每个场景之前使用 database_cleaner gem 来截断测试数据库
我目前在运行 PostgreSQL 的 Rails 项目中使用 DatabaseCleaner,并将其设置如下。 RSpec.configure do |config| config.before
从来源(database_cleaner,active_record)来看,它们应该同样快。但是有人声称使用 database_cleaner 的事务策略会降低 Controller 和模型规范的速度
我有一个使用 rspec 并包含 DatabseCleaner 的 rails 应用程序,以确保每次测试之间的测试数据库都是干净的。 DatabaseCleaner 在我们的 spec/rails_h
我是一名优秀的程序员,十分优秀!