When checking ActiveRecord, do I get confirmation? => false, but there are no errors in error.count. How so?

When creating ActiveRecord in rspec, I use instruments for valid records. But when using fxitures in tests, they don't seem to check. In the following example, the employee looks perfectly correct, and yet the related check in the specification says that they are not valid.

class Employee < ActiveRecord::Base
  validates_presence_of     :email
end

class User < ActiveRecord::Base
  validates_associated      :employee
end

#Command line debugger in 'debugger' (below) returns:
Employee.find(745185059).errors.count         # => 0
Employee.find(745185059).errors.full_messages # => []
Employee.find(745185059).valid?               # => true

Example:

describe SessionsController do
  fixtures :users, :employees

  describe "Logging in by cookie" do
    def set_remember_token token, time
      @user[:remember_token]            = token; 
      @user[:remember_token_expires_at] = time
      debugger
      @user.save! # THIS SAYS THE EMPLOYEE IS INVALID, but why, if the fixtures are good?
    end    
    it 'logs in with cookie' do
      stub!(:cookies).and_return({ :auth_token => 'hello!' })
      logged_in?.should be_true
    end
  end
end

Any ideas why I get a validation failure? (Of course, I cut a lot of middle code, it is possible that the error is somewhere else)

+3
source share
1 answer

@user .save( !), , , , , . :

@user.save
puts @user.errors.inspect # or your command line debugger

.

+5

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


All Articles