Factory Girl Devise Factory user is invalid - RSpec test fails

I am new to programming and have been learning Ruby on Rails for 11 weeks.

When I try to check my factory user to check, I get

1) User has a valid factory Failure/Error: expect(@user.valid?).to eq(true) expected: true got: false (compared using ==) 

I use Devise for my user model and FactoryGirl for my factories.

Here is my factory user:

 FactoryGirl.define do factory :user do name "John Fahey" sequence(:email, 100) { |n| "person#{n}@example.com" } password "password" password_confirmation "password" confirmed_at Time.now end end 

... and here is my specification

 require 'rails_helper' describe User do before do @user = build(:user) end it "has a valid factory" do expect(@user.valid?).to eq(true) end end 

I have long been working to ensure that this specification passes. For some time I received the error "letter is already taken", and I received it. I even got the specification to go through once, but I used the obsolete "should be" syntax. When I get to the correct syntax ": wait", I get this error. Does anyone have an idea of ​​what I'm doing wrong here?

Here is my model

 class User < ActiveRecord::Base #== schema information. # create_table "users", force: true do |t| # t.string "name" # t.string "email", default: "", null: false # t.string "encrypted_password", default: "", null: false # t.string "reset_password_token" # t.datetime "reset_password_sent_at" # t.datetime "remember_created_at" # t.integer "sign_in_count", default: 0, null: false # t.datetime "current_sign_in_at" # t.datetime "last_sign_in_at" # t.string "current_sign_in_ip" # t.string "last_sign_in_ip" # t.string "confirmation_token" # t.datetime "confirmed_at" # t.datetime "confirmation_sent_at" # t.string "unconfirmed_email" # t.datetime "created_at" # t.datetime "updated_at" # end # add_index "users", ["email"], name: "index_users_on_email", unique: true # add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true has_one :list has_many :items, through: :list devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable end 

This is what I see when I create a user instance in the rails console and check for errors:

 2.0.0-p576 :004 > @user = User.create (0.2ms) begin transaction (0.2ms) rollback transaction => #<User id: nil, name: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_coun t: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sen t_at: nil, unconfirmed_email: nil, created_at: nil, updated_at: nil> 2.0.0-p576 :005 > @user.save (1.0ms) begin transaction (0.1ms) rollback transaction => false 2.0.0-p576 :006 > puts(@user.errors) #<ActiveModel::Errors:0xa7cff88> => nil 2.0.0-p576 :007 > 

When I print error messages, I get

 2.0.0-p576 :007 > puts(@user.errors.messages) {:email=>["can't be blank"], :password=>["can't be blank"]} => nil 

something interesting here. Just to make sure, I did rake db: migrate and rake db: test: prepare. The test has passed. Then I repeated the exact test and it failed.

 vagrant@rails-dev-box :~/code/blocitoff$ rake db:migrate vagrant@rails-dev-box :~/code/blocitoff$ rake db:migrate vagrant@rails-dev-box :~/code/blocitoff$ rake db:test:prepare vagrant@rails-dev-box :~/code/blocitoff$ rspec spec/models/user_spec.rb #<ActiveModel::Errors:0xbdf0740> . Finished in 0.1764 seconds (files took 8.96 seconds to load) 1 example, 0 failures vagrant@rails-dev-box :~/code/blocitoff$ rspec spec/models/user_spec.rb #<ActiveModel::Errors:0xa97066c> F Failures: 1) User has a valid factory Failure/Error: expect(@user.valid?).to eq(true) expected: true got: false (compared using ==) 
+5
source share
2 answers

The email attribute must be unique, and you do not clear your database between tests, so you get an error during the second and subsequent execution of your test. See https://relishapp.com/rspec/rspec-rails/docs/transactions to learn about using transactions in RSpec.

+2
source

Just to pack what Peter said, I was able to solve my problem by adding this parameter to my spec_helper.rb.

 RSpec.configure do |config| config.use_transactional_fixtures = true end 
+1
source

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


All Articles