Ruby on rails 关于RSpec测试中的事务使用

Ruby on rails 关于RSpec测试中的事务使用,ruby-on-rails,ruby-on-rails-3,factory-bot,rspec2,database-cleaner,Ruby On Rails,Ruby On Rails 3,Factory Bot,Rspec2,Database Cleaner,在使用RSpec编写测试时,我遇到了一个非常奇怪的问题。假设我有两个模型:Company和Item with association Company有许多项。我还使用策略事务设置了数据库。我的RSpec版本是2.13.0,database_cleaner版本是1.0.1,rails版本是3.2.15,factory_girl版本是4.2.0。这是我的测试: let(:company) { RSpec.configuration.company } context "has accounts" d

在使用RSpec编写测试时,我遇到了一个非常奇怪的问题。假设我有两个模型:Company和Item with association Company有许多项。我还使用策略事务设置了数据库。我的RSpec版本是2.13.0,database_cleaner版本是1.0.1,rails版本是3.2.15,factory_girl版本是4.2.0。这是我的测试:

let(:company) { RSpec.configuration.company }
context "has accounts" do
  it "returns true" do
    company.items << FactoryGirl.create(:item)
    company.items.count.should > 0
  end
end
context "does not have accounts" do
  it "returns false" do
    company.items.count.should == 0
  end
end
let(:company){RSpec.configuration.company}
上下文“有帐户”吗
它“返回真”吗
公司项目0
终止
终止
上下文“没有帐户”是否存在
它“返回false”吗
company.items.count.should==0
终止
终止
结束


我设置了一个初始的company-to-rspec配置,以便在每个测试中使用,因为我不想在每个测试中重新创建它,因为创建一个company需要很多时间(由于它的回调和验证)。第二个测试失败,因为在第一个测试之后未从数据库中清除项。我不明白为什么。如果我更改行
company.items我认为问题不在回滚中,我想知道
company.items
是否可以在
上下文
之间存储它的值,但我不确定

我无法快速复制,因此我想请您:

  • 执行回滚时,请检查
    log/test.log

  • company.items
    RSpec.configuration.company
    INSERT
    s创建了多少个
    RSpec.configuration.company=FactoryGirl.create(:company)
    ,我通过
    config.add\u设置:company
    向RSpec添加了设置,我不知道如何在注释中编写代码…您可以随时编辑您的问题以提供更多详细信息
    let(:item) { FactoryGirl.build(:item) }
    context "has accounts"
    it "returns true" do
      item.save
      Company.items.count.should == 1
    end
    
    RSpec.configure do |config|
      # most omitted
      config.include FactoryGirl::Syntax::Methods
    
    let(:item) { build(:item) }