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

Unit Test ReadMe update #150

Closed mnreggi closed 3 years ago

mnreggi commented 3 years ago

Hello awesome Team! I don't know if this is the best way of asking this, but through this PR https://github.com/github/Scientist.net/pull/108, you added the option for IoC/Dependency Injection. The readme doesn't say anything about running UnitTest on classes that use Scientist.

For Example (This case is using the static approach, but my case uses the Injected one .Experiment):

public decimal GetUserStatistic(IUser user)
{
    return Scientist.Science<decimal>("new-statistic-calculation", experiment =>
    {
        experiment.RunIf(() => user.IsTestSubject);

        experiment.Use(() => CalculateStatistic(user));
        experiment.Try(() => NewCalculateStatistic(user));
    });
}

Thanks!

JoshHiles commented 3 years ago

Thank you for getting in touch!

Your first point (and anyone correct me on any of the answers below, too busy doing devops & angular ☹️), your unit tests for CalculateStatistic(user) will still stay the same as that is a unit of code (method) which needs testing independently.

For the method CalculateStatistic(user) it depends on where it lives as to whether or not it needs to be private or public, if its in the same class it can be private or public if its in another class. In regards to unit testing a private method you can use the following i think

Class target = new Class();
PrivateObject obj = new PrivateObject(target);
var retVal = obj.Invoke("PrivateMethod");
Assert.AreEqual(expectedVal, retVal);

You can run the GetUserStatistic() method but as far as unit testing goes, you would mock out the methods CalculateStatistic() & NewCalculateStatistic() because those are seperate unit's of code.

Hopefully i've digested what you said correctly & answered your questions! If anyone else can comment just to confirm or tell me im chatting out my backside then feel free 😄

mnreggi commented 3 years ago

Legend! Thanks for that, appreciate the quick response.