Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ActiveSupport::EnvironmentInquirer example
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
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
@joemasilotti
Copy link
Author

joemasilotti commented Dec 22, 2021

TIL about ActiveSupport::EnvironmentInquirer, which is great for injecting an environment-dependent conditional for tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment