vanderkleij/Smocks

private functions are unable to setup context correctly, unless same setup is declare within Smock.Run

HanaKana opened this issue · 2 comments

[Test]
public void Dummy()
{
    Smock.Run(context =>
    {
        PrivateDummySetup(context);
        var date = DateTime.Now;
        var breakAfter = 123;

        //line is required to 'setup' the datetime.now value correctly in the private function
        //leaving commented out will result in no shim for datetime
        //leaving line in will result in private function setup working. even though function is declared after
//                context.Setup(() => DateTime.Now).Returns(new DateTime(2009));
        var date2 = DateTime.Now;
        var breakAfter2 = 123;
    });
}

private void PrivateDummySetup(ISmocksContext context)
{
    context.Setup(() => DateTime.Now).Returns(new DateTime());
    var date = DateTime.Now;
    var breakAfter = 123;
}

Note: using a second setup function on datetime.now will return the first setup returning.

This is currently by design. Smocks only scans the lambda provided to Smock.Run for Setup calls (see how Smocks works). As an improvement, it could additionally scan methods in the same assembly that are invoked by this lambda. I'll keep this issue around as a reminder.

For now, please put all Smock.Run invocations directly within the lambda body provided to Smock.Run.

This is now fixed: Smocks follows calls to methods within the same class and scans those for Setup calls as well. This way, the setup in the PrivateDummySetup method will be detected by Smocks.