Rex*_*Rex 18 ruby rspec ruby-on-rails fixtures factory-bot
与此处描述的问题类似:http: //rpheath.com/posts/411-how-to-use-factory-girl-with-rspec
简而言之(缩短代码):
spec_helper:
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
Run Code Online (Sandbox Code Playgroud)
factories.rb:
Factory.define :state do
f.name "NY"
end
Run Code Online (Sandbox Code Playgroud)
在我的规格中
before(:each) do
@static_model = Factory(:state) # with validate uniqueness of state name
end
Run Code Online (Sandbox Code Playgroud)
错误:
重复的条目名称"NY"等.
问题:在每个规范示例之前,不应该rspec清除数据库,因此不会抛出重复的条目错误吗?
nat*_*vda 40
我想的事情:
rake spec用来运行你的测试套件:从头开始构建数据库(以确保没有任何东西粘在一起)before (:all)?因为你在a中创建的任何内容都before :all应该在a中再次删除after :all或者保持现有状态.