[英]database_cleaner not clearing some of my tests (rspec, capybara, selenium)
database_cleaner works for the most part but on some things where i am expecting something like user_id to clear after each test it doesn't. database_cleaner在大多数情况下都有效,但在某些情况下,我希望每次测试后都无法清除类似user_id的内容。 so, the user_id will increment throughout instead of clearing and the user id is predictable as 1, 2, 3 or however many that are created for the test.
因此,user_id将一直递增而不是清除,并且用户ID可以预测为1、2、3或为测试创建的数量。 i can just call the id instead of hardcoding the expected result but later on i really need it to clear that stuff in more complex examples.
我可以只调用id而不是对预期结果进行硬编码,但是稍后我真的需要它来清除更复杂示例中的内容。 this is the easiest for showing.
这是最容易显示的。 any help would be greatly appreciated.
任何帮助将不胜感激。
FROM SPEC_HELPER.RB: 从SPEC_HELPER.RB:
RSpec.configure do |config|
config.mock_with :rspec
config.include FactoryGirl::Syntax::Methods
config.include(Capybara, :type => :integration)
config.include Devise::TestHelpers, :type => :controller
config.use_transactional_fixtures = false
config.before(:each) do
I18n.default_locale = :en
I18n.locale = :en
DatabaseCleaner.start
ResqueSpec.reset!
ActionMailer::Base.deliveries.clear
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after(:all) do
TestCleaner.clean
end
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.strategy = :transaction
Role.reset_cache!
end
config.after(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
FROM MY TEST: 从我的测试:
it "should return one provider" do
get :index
response.body.gsub(/\s+/, "").should == {
:experts => [{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 1,
:photo => @provider.details.photo.url
}] }.to_json
end
it "should show return two providers" do
@provider2 = create(:provider)
get :index
response.body.gsub(/\s+/, "").should == {
:experts => [{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 1,
:photo => @provider.details.photo.url
},
{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 2,
:photo => @provider.details.photo.url
}
] }.to_json
end
Database cleaner is wrapping each of your specs in a transaction, and rolling back that transaction at the end of the spec to remove any database changes. 数据库清理器将您的每个规范包装在一个事务中,并在规范末尾回滚该事务以删除任何数据库更改。 Rolling back a transaction doesn't reset the autoincrement or sequence values used to auto assign the primary key though.
但是,回滚事务不会重置用于自动分配主键的自动增量或序列值。
I'd strongly recommend not hardcoding the ids. 我强烈建议您不要对ID进行硬编码。 You mention that your examples will get more complicated, in which case having random integers sprinkled through your code will be even less maintainable than in simpler examples.
您提到您的示例将变得更加复杂,在这种情况下,与简单示例相比,在代码中散布随机整数将更加难以维护。 Assuming you're using mysql then using the truncation strategy will reset autoincrement values, but it is also a lot slower.
假设您使用的是mysql,则使用截断策略将重置自动增量值,但速度也慢得多。
Is it the request/integration specs where the database is not cleaning properly? 是数据库无法正确清理的请求/集成规范? If so it's possibly because you're cleaning with a transaction strategy and not a truncation strategy.
如果是这样,则可能是因为您正在使用事务处理策略而不是截断策略进行清理。
Try adding this, which changes the strategy to truncation for integration specs only: 尝试添加它,这会将策略更改为仅针对集成规范的截断:
config.before type: :integration do
DatabaseCleaner.strategy = :truncation
end
You can't use a transaction strategy for integration specs because Capybara runs in a separate thread with a different database connection. 您不能将事务策略用于集成规格,因为Capybara在具有不同数据库连接的单独线程中运行。
This article helped me a lot in setting up my DB stuff for rspec/capybara: Sane Rspec config for clean, and slightly faster, specs . 本文为我为rspec / capybara设置数据库资料提供了很多帮助: Sane Rspec配置用于清理规范,并且速度稍快 。
I ran into this a lot when starting testing. 开始测试时,我遇到了很多问题。 I eventually found that it was not due to the database cleaner issue (as I also suspected), but rather the structure of the code that does the tests.
最终,我发现这不是由于数据库清理程序的问题(我也怀疑),而是由于执行测试的代码的结构。
The best way I can try and descibe is is to say that basically if you do things outside of your before(:each)
set up blocks and the actual it
and should
it end up being 'outside' of the actual test and causes these issues. 我可以尝试和descibe是最好的办法是说,基本上,如果你做的事情你之外的
before(:each)
成立块,实际it
和should
它最终会被实际测试的“外部”,并导致这些问题。
Specifically I suspect that there could be a problem here: 我特别怀疑这里可能存在问题:
it "should show return two providers" do
@provider2 = create(:provider)
get :index
response.body.gsub(/\s+/, "").should == {
I would look to change this to something more like: 我希望将其更改为以下内容:
it "should show return two providers" do
before(:each) do {
@provider2 = create(:provider)
get :index
}
response.body.gsub(/\s+/, "").should == {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.