scientistproject / Scientist.net

A .NET library for carefully refactoring critical paths. It's a port of GitHub's Ruby Scientist library
MIT License
1.46k stars 95 forks source link

[Question] How to test access to global/session variables? #91

Closed danielepo closed 7 years ago

danielepo commented 7 years ago

As you may have guessed I work in a really ugly codebase :-)

Is there a way to verify the access to global variables during the execution an experiment?

joncloud commented 7 years ago

If you have something that the access will change from global/session for comparison, then I think Scientist can help. If your app is using session, then you might be storing something like the current user in there. This is an example of a refactor that could be made to hook Scientist up to say load the User from the database by name instead of relying on session state.

IUser GetUser(string username) {
    return Scientist.Science<bool>(nameof(GetUser), experiment =>
    {
        experiment.Use(() => (IUser)Session["CurrentUser"]);
        experiment.Try(() => LoadUserFromDb(username));
    });
}

Note that to gain the full advantage of Scientist things like tracking results, custom comparisons, enabling observations, etc., there is additional wiring that can be found in the README.md.