Start learning free

LESSON 144 OF 173 · BACKEND: TESTS AND RUNNING IT

Settings per Environment

Practice

Your shop runs in two places with the same code: on your computer while you build it, the development environment, and on the server where customers use it, production. Only a few settings differ. The server tells the program where it runs with an environment variable, a named text value that every program it starts can read.

You read one with Environment.GetEnvironmentVariable, which gives null when it isn't set; ?? then picks the default on its right. In a test, PatchEnv, a helper this lesson gives you, sets a variable only for the lines inside a using block, and puts the old value back when the block ends. Here a shop picks its currency:

var currency = Environment.GetEnvironmentVariable("SHOP_CURRENCY") ?? "USD";

using (PatchEnv("SHOP_CURRENCY", "EUR")) {
    Console.WriteLine(Environment.GetEnvironmentVariable("SHOP_CURRENCY"));   // EUR
}

The setting that matters most is debug mode, the shop's app.Configuration["DEBUG"]. The shop uses it to pick its error page: with it on, a crash answers with a developer page showing the error, the lines of code it went through and the request's details. Handy on your computer, a disaster on the server. So it must be on only when SHOP_ENV is exactly development, and off for production, for a missing variable and for a typo. Configuration values are text, so DEBUG holds "True" or "False".

The check starts your shop with SHOP_ENV=development, calls your method in each case, and runs your test on the shop and on a copy that keeps debug mode on everywhere. It must print:

Your shop, started on your computer with SHOP_ENV=development:
  DEBUG = True
LoadConfig on your computer, with SHOP_ENV=development:
  DEBUG = True
LoadConfig on the server, with SHOP_ENV=production:
  DEBUG = False
LoadConfig on a server where nobody set SHOP_ENV:
  DEBUG = False
LoadConfig with SHOP_ENV=Development, a typo:
  DEBUG = False
Your tests on the shop:
  debug off in production ... ok
  1 test: OK
Your tests on a copy that keeps debug mode on everywhere:
  debug off in production ... FAIL
    Assert.Equal() Failure: Expected "False", Actual "True"
  1 test: 1 failed, so they caught this bug

Your task: write LoadConfig(WebApplication app), which reads SHOP_ENV with the default "production" and sets app.Configuration["DEBUG"]; the starting code already calls it for the shop's app. Then write the test "debug off in production", which loads the settings into a new app with SHOP_ENV set to production and checks that debug mode is off.


Assignment
Follow the Coding Tutorial to load the shop's settings from SHOP_ENV and write the test that keeps debug mode off in production.


Hint
A comparison is already true or false, and .ToString() turns it into the text "True" or "False". In the test, make a new app with WebApplication.CreateBuilder().Build() instead of changing the shop's, call LoadConfig inside the using block, and check the setting below it with Assert.Equal.


STEP 1 OF 8
    Ctrl + Enter to check