剛從測試的sqlite
切換到pg
。我注意到,雖然測試表送走就好了,該ID不重啓1。所以換句話說,給出的測試:使用Postgres測試環境的Rails規範,測試表不會重新啓動id增量?
describe "" do
before do
# some dummy is created
Object.create()
puts "count = #{Object.count}"
puts "last object_id = #{Object.id}"
end
...
end
如果您運行許多測試,你會得到下面的輸出:
count = 1
last object_id = 1
count = 1
last object_id = 2
count = 1
last object_id = 3
count = 1
last object_id = 4
而我覺得這是更好地都是清晰的測試表,並重新啓動ID,以便它:
count = 1
last object_id = 1
count = 1
last object_id = 1
count = 1
last object_id = 1
count = 1
last object_id = 1
我以爲這只是我需要在我spec_helper
文件?作爲參考,下面是當前的幫助程序文件,我正在使用database_cleaner
寶石。謝謝!
RSpec.configure do |config|
# Database cleaner set up below
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# Clean up all jobs specs with truncation
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
通常,對象的id與實際測試無關,因此重新設置自動增量不是必需的。 – Rafal
您確定使用截斷策略運行測試嗎?我希望在測試的頭文件中使用'describe'「,js:true',所以我懷疑你可能會用事務策略來運行測試。您可以在測試運行時顯示測試日誌嗎?在那裏你應該清楚地看到它是否執行了事務'ROLLBACK'或者截斷。 – BoraMa