ruby on rails - Trying to change environment variable within RSpec model spec -


i've got 1 user model has different validations based on environmental variable env['app_for']. can either "app-1" or "app-2". app-1 validates username while app-2 validates email address. here user model spec app-1:

require 'rails_helper'  rspec.describe user, type: :model    include shared::categories    before     env['app_for']='app-1'   end    context "given valid user"     before { allow_any_instance_of(user).to receive(:older_than_18?).and_return(true) }      {should validate_presence_of :username}   end end  

and user model spec app-2

require 'rails_helper'  rspec.describe user, type: :model    include shared::categories    before     env['app_for']='app-2'   end    context "given valid user"     before { allow_any_instance_of(user).to receive(:older_than_18?).and_return(true) }      {should validate_presence_of :email}   end end 

my problem environment variable isn't being set expect in before block. ideas on how this?

edit 1

here validation implementation. used concern extend user model with:

module topdogcore::concerns::uservalidations   extend activesupport::concern   included      if env['app_for'] == 'app-1'       validates :username,                 presence: true,                 uniqueness: true              elsif env['app_for'] == 'app-2'       validates :email,                  presence: true,                 uniqueness: true     end   end end 

try it

module topdogcore::concerns::uservalidations   extend activesupport::concern   included      validates :username,       presence: true,       uniqueness: true, if: -> { env['app_for'] == 'app-1' }      validates :email,       presence: true,       uniqueness: true, if: -> { env['app_for'] == 'app-2' }   end end 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -