我有一個基於環境變量ENV ['APP_FOR']具有不同驗證的用戶模型。這可以是「app-1」或「app-2」。 app-1驗證用戶名,而app-2驗證電子郵件地址。下面是APP-1我的用戶模型規格:試圖在RSpec模型規範中更改環境變量
require 'rails_helper'
RSpec.describe User, type: :model do
include Shared::Categories
before do
ENV['APP_FOR']='app-1'
end
context "given a valid User" do
before { allow_any_instance_of(User).to receive(:older_than_18?).and_return(true) }
it {should validate_presence_of :username}
end
end
這是用戶模型規範APP-2
require 'rails_helper'
RSpec.describe User, type: :model do
include Shared::Categories
before do
ENV['APP_FOR']='app-2'
end
context "given a valid User" do
before { allow_any_instance_of(User).to receive(:older_than_18?).and_return(true) }
it {should validate_presence_of :email}
end
end
我的問題是環境變量沒有被設置爲我預計它會在之前的區塊中。任何想法如何做到這一點?
編輯1
這是我的驗證實現。我用了一個關心我與擴展用戶模型:
module TopDogCore::Concerns::UserValidations
extend ActiveSupport::Concern
included do
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
你可以發佈您的驗證實現? –
@IgorBelo查看上面的新編輯,我已將它包含在那裏 – NdaJunior