Ruby on rails 自动测试和Factory Girl的验证问题

Ruby on rails 自动测试和Factory Girl的验证问题,ruby-on-rails,ruby,factory-bot,rspec2,autotest,Ruby On Rails,Ruby,Factory Bot,Rspec2,Autotest,我的自动测试有问题。在我的用户模型中,用户名和电子邮件地址是唯一的。当我开始自动测试时,一切正常。在第二轮自动测试中,我有一个 验证失败:电子邮件已发送,用户名已发送 仅在两次测试中出错。我不明白为什么,我使用带有序列的factorygirl,每次都会生成一个新用户名 这是我的rspec2文件: describe User do specify { Factory.build(:user).should be_valid } context "unique values" do

我的自动测试有问题。在我的用户模型中,用户名和电子邮件地址是唯一的。当我开始自动测试时,一切正常。在第二轮自动测试中,我有一个

验证失败:电子邮件已发送,用户名已发送

仅在两次测试中出错。我不明白为什么,我使用带有序列的factorygirl,每次都会生成一个新用户名

这是我的rspec2文件:

describe User do

  specify {  Factory.build(:user).should be_valid }

  context "unique values" do

    before :all do
       @user = Factory.create(:user)
    end


    it "should have an unique email address" do
       Factory.build(:user, :email  => @user.email).should_not be_valid
    end

    it "should have an unique username" do
       Factory.build(:user, :username  => @user.username).should_not be_valid
    end

  end


  context "required attributes" do

    it "should be invalid without an email address" do
       Factory.build(:user, :email => nil).should_not be_valid
    end

    it "should be invalid without an username" do
       Factory.build(:user, :username  => nil).should_not be_valid
    end

    it "should be invalid without an password" do
       Factory.build(:user, :password  => nil).should_not be_valid
    end

    it "should be invalid without an address" do
        Factory.build(:user, :address  => nil).should_not be_valid
    end


  end
end
工厂:

Factory.sequence :username do |n|
  "Outfit#{n}er"
end

Factory.sequence :email do |n|
  "user#{n}@example.com"
end

Factory.define :user do |u|
  u.username              { Factory.next :username }
  u.email                 { Factory.next :email }
  u.password              'secret'
  u.phone_number          '02214565854'
  u.association :address, :factory => :address
end

Factory.define :confirmed_user, :parent => :user do |u|
  u.after_build { |user| user.confirm! }
end
只有两个测试在“唯一值”上下文中不起作用。所有其他测试都可以正常工作


谢谢

问题可能出在您的配置文件中。在spec config文件(可能是spec_helper.rb)中有一行内容如下:

config.use_transactional_fixtures = false config.use\u transactional\u fixtures=false 这应该是:

config.use_transactional_fixtures = true config.use\u transactional\u fixtures=true 或者,如果您不想使用事务性装置,您应该在测试中使用before(:each)函数:

before :each do User.destroy_all end 以前:每个人都做 User.destroy\u all 结束
或者,我使用我的工厂,因为它与非事务性装置和序列更兼容。

这正是我刚刚遇到的问题——我有一个奇怪的经历,如果我只是运行“rake spec”,这个问题似乎不会发生

在没有找到明显的答案后,我尝试了一些似乎解决了问题的方法!如果使用FactoryGirl,在before()块中设置类变量似乎是罪魁祸首

我有一些非常相似的东西:

factory :widget do
  sequence(:name) { |n| "widget#{n}" }
end
然后:

自动测试将发现唯一性约束问题。我将rspect测试更改为:

describe Widget do
  let(:widget) { FactoryGirl.create(:widget) }
  it ...yadda yadda...
    widget.foo()
  end
end

…唯一性约束问题消失了。不确定为什么会发生这种情况,看起来不应该发生,但可能是一个可行的解决方案。

我之所以会遇到这个问题,是因为在一些测试之后数据库没有被正确清除

如果您运行: rake数据库:测试:准备 在自动测试之前,它还会发生吗

我最终通过隔离哪些测试没有被清除并修复它们(或删除它们)来绕过它。对不起,我不记得是什么问题,但它有一些错误导致它

哦,我在没有spork的情况下运行了它,这样我就可以看到我的日志消息与结果混合在一起


希望这能有所帮助。

我遇到了一个非常类似的问题,我尝试了一些关于这个主题的建议,但没有成功。但是,我确实通过在spec_helper.rb中添加以下内容来修复它:

# Clean up the database
  require 'database_cleaner'
   config.before(:suite) do
     DatabaseCleaner.strategy = :truncation
     DatabaseCleaner.orm = "mongoid"
  end

  config.before(:each) do
    DatabaseCleaner.clean
  end

另外,不要忘记将DatabaseCleaner gem添加到您的gem文件中

谢谢您的回答。但是我在spec\u helper.rb中有“config.use\u transactional\u fixtures=true”
# Clean up the database
  require 'database_cleaner'
   config.before(:suite) do
     DatabaseCleaner.strategy = :truncation
     DatabaseCleaner.orm = "mongoid"
  end

  config.before(:each) do
    DatabaseCleaner.clean
  end