ActiveSupport::EnvironmentInquirer example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SidekiqPolicy | |
attr_reader :user, :environment | |
def initialize(user, environment: Rails.env) | |
@user = user | |
@environment = environment | |
end | |
def visible? | |
user.admin? || environment.development? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test_helper" | |
class SidekiqPolicyTest < ActiveSupport::TestCase | |
test "only admins can see the route" do | |
assert SidekiqPolicy.new(users(:admin)).visible? | |
refute SidekiqPolicy.new(users(:empty)).visible? | |
end | |
test "everyone can see the route in development" do | |
environment = ActiveSupport::EnvironmentInquirer.new("development") | |
assert SidekiqPolicy.new(users(:admin), environment: environment).visible? | |
assert SidekiqPolicy.new(users(:empty), environment: environment).visible? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TIL about
ActiveSupport::EnvironmentInquirer
, which is great for injecting an environment-dependent conditional for tests.