Verification Problem with Autotest and Factory Girl

I have a problem with Autotest. My user model has a unique username and email address. When I run Autotest, everything works fine. In the fourth round, from the autotest, I have

Verification failed: email has already been done, username has already been completed

error in only two tests. I do not understand why, I use factorygirl with sequenze, and this should generate a new username each time.

This is my rspec2 file:

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:

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

Only two tests that do not work are in the context of "unique values". All other tests work without errors.

thank

+3
source share
4 answers

, - , , "rake spec".

, , -, , , ! , before() , FactoryGirl.

- :

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

describe Widget do
  before(:each) { @widget = FactoryGirl.create(:widget) }
  it ...yadda yadda...
    @widget.foo()
  end
end

autotest . rspect:

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

... . , , , , .

+1

- .

: rake db: test: ?

, , ( ). , , , .

, spork, , , .

, .

+1

, , . spec (, spec_helper.rb), :

config.use_transactional_fixtures = false

:

config.use_transactional_fixtures = true

, , before (: each):

before :each do
  User.destroy_all
end

As an alternative, I use "Sham gem" for my factories because it is more compatible with non-transactional lights and sequences.

0
source

I ran into a very similar problem and I tried a few suggestions on this topic without any luck. However, I fixed it by adding the following to my 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

Also remember to add the DatabaseCleaner stone to your gemfile

0
source

Source: https://habr.com/ru/post/1783513/


All Articles