- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我对 RSpec 一直坚持使用基于 xUnit 的测试框架有点迷茫,但我正在试一试。
规范编写方式的嵌套性质让我有些头疼,因为我应该在哪里进行数据库设置/拆卸。
根据 DatabaseCleaner自述文件:
Spec::Runner.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
现在我不能使用事务,因为我在我的代码中使用它们,所以我只是坚持截断,但这不应该在这里或那里。
我有这个:
RSpec.configure do |config|
config.mock_with :rspec
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
这里的问题是,当我尝试在以下 subject
或 let
block 中创建的任何 fixtures 已经消失(从数据库中)时 describe
或 it
block 。
例如(使用 Machinist 创建夹具...但这不应该是相关的):
describe User do
describe "finding with login credentials" do
let(:user) { User.make(:username => "test", :password => "password") }
subject { User.get_by_login_credentials!("test", "password") }
it { should == user }
end
end
我正在为我应该如何嵌套这些 describe
和 subject
以及其他 block 而苦苦挣扎,所以也许这是我的问题,但基本上这失败了,因为当它试图从数据库中获取用户,由于调用了 after(:each)
Hook ,它已被删除,大概是在 let
?
最佳答案
如果您要一起使用 subject
和 let
,您需要了解如何/何时调用它们。在这种情况下,subject
在 let
生成的 user
方法之前被调用。问题不在于对象在 subject
被调用之前从数据库中删除,而是它甚至没有在那个时候创建。
如果您使用 let!
方法,您的示例将工作,该方法添加一个 before
Hook ,隐式调用示例之前的 user
方法(因此在调用 subject
之前)。
也就是说,我建议您停止挣扎并使用 RSpec 已经公开的更简单的 API:
describe User do
it "finds a user with login credentials" do
user = User.make(:username => "test", :password => "password")
User.get_by_login_credentials!("test", "password").should eq(user)
end
end
这对我来说似乎更简单。
关于ruby-on-rails - RSpec + DatabaseCleaner 帮助——拆卸过早发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111909/
救世主。 在每个 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
我是一名优秀的程序员,十分优秀!