我有一个基于环境变量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