chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello all,

I want to make a test case for a method which will return screen information based on certain paramerters e.g (screenName, module, context). But inside this method I have a scenario like :

object Screen;

byte acl = GetScreenACL(screen, module, contextCode, uow, out Screen);

GetScreenACL is a private static method of some class e.g abc.

Can any one help me, how should I mock GetScreenACL so that I can get both Screen and acl.

Thanks
asked by sony (7.2k points)

1 Answer

0 votes
Hi Sony,

For faking private methods use Isolate.NonPublic.WhenCalled(...)
And for checking the arguments use the the DoInstead completing statement.
Below is a simple example:

// code under test
public class Foo
{
    public static int Bar(string s)
    {
        return StaticMethod(s);
    }
    
    // here is the method we want to fake
    private static int StaticMethod(string s)
    {
        return 1;
    }
}

[TestFixture, Isolated]
public class Tests
{
    [Test]
    public void Test()
    {
        Isolate.NonPublic.WhenCalled(typeof(Foo), "StaticMethod").
            DoInstead(context =>
                    {
                        string s = context.Parameters[0] as string;
                        if (s == "a")
                        {
                            return 5;
                        }

                        return 4;
                    });
        
        Assert.AreEqual(4, Foo.Bar("b"));
    }
}


:arrow: To get access to the parameters values in run time use the context.Parameters[0] and cast them to the appropriate type.
answered by ohad (35.4k points)
...