Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::NotNullViolation on specs #298

Closed
fladson opened this issue Feb 17, 2018 · 2 comments
Closed

ActiveRecord::NotNullViolation on specs #298

fladson opened this issue Feb 17, 2018 · 2 comments

Comments

@fladson
Copy link

fladson commented Feb 17, 2018

Hi, first thanks for the great work on this, it's very easy to setup.

I am using this on a devise model with rails 5 and so far it's all working fine, the only issue is when I try to create/save a record on the specs.

This is what I have:
schema

  create_table "users", force: :cascade do |t|
    ...
    t.string "encrypted_session", default: "", null: false
    t.string "encrypted_session_iv"
  end

model

class User < ApplicationRecord
  attr_encrypted :session, key: Rails.application.secrets.secret_key_attr_encrypt
  validates :session, presence: true
end

spec

require "rails_helper"

RSpec.describe User, type: :model do
  it { is_expected.to validate_presence_of(:session) }

  describe ".session encryption" do
    # this is fine
    it "assigns an encrypted value to encrypted_session and encrypted_session_iv" do
      user = User.new(session: Faker::Internet.password)

      expect(user.encrypted_session).not_to be_empty
      expect(user.encrypted_session_iv).not_to be_empty
    end

    it "returns the correct value after save" do
      session = Faker::Internet.password
      user = build(:user, session: session)
      binding.pry
      user.save

      expect(user.session).to eq(session)
    end
  end
end

the pry inspection

[1] pry(#<RSpec::ExampleGroups::User::SessionEncryption>)> user
=> #<User id: nil, email: "[email protected]", created_at: nil, updated_at: nil, session: nil>
[2] pry(#<RSpec::ExampleGroups::User::SessionEncryption>)> session
=> "Vp4q8fOoL3Np"
[3] pry(#<RSpec::ExampleGroups::User::SessionEncryption>)> user.session
=> "Vp4q8fOoL3Np"

The error

Failure/Error: user.save

     ActiveRecord::NotNullViolation:
       PG::NotNullViolation: ERROR:  null value in column "session" violates not-null constraint

The console and controller creation is fine
I've tried to put attribute :session at the model, but no success

Thanks for the help

@chriscortez
Copy link

@fladson Any luck figuring this error out?

@wallace
Copy link

wallace commented Apr 19, 2019

This seems to be related to #335, i.e. how the attribute is assigned according to Rails attribute semantics.

@fladson fladson closed this as completed Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants