【发布时间】:2014-08-03 10:42:34
【问题描述】:
rails 代码库中有 cmets 指示应该在运行之间重置测试数据库
耙-T
rake test:all # Run tests quickly by merging all types and not resetting db
rake test:all:db # Run tests quickly, but also reset db
config/database.yml
# 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:
对我来说似乎不是这样。
我正在使用工厂女孩生成测试模型,这里是一个示例工厂
FactoryGirl.define do
factory :podcast do
sequence(:title) { |n| "Podcast #{n}" }
sequence(:feed_url) { |n| "http://podcast.com/#{n}" }
end
end
播客应该有一个唯一的 feed_url,所以我验证它在模型中的唯一性。
class Podcast < ActiveRecord::Base
validates :feed_url, uniqueness: true, presence: true
end
在test_helper.rb我lint所有工厂
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/autorun'
FactoryGirl.lint
我的测试创建了一个播客,构建了另一个同名的播客,然后断言第二个 无效。
require 'test_helper'
describe Podcast do
describe '#feed_url' do
it 'must be unique' do
podcast = create(:podcast)
new_podcast = build(:podcast, feed_url: podcast.name)
assert_invalid podcast, :feed_url, 'has already been taken'
end
end
end
我第一次运行测试时,它执行时没有错误并且测试全部通过。 我第二次运行测试时,Factory Girl lint 失败,因为播客 feed_url 已经被占用。
为什么没有在两次运行之间重建测试数据库?
【问题讨论】:
标签: ruby-on-rails testing factory-bot minitest