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
I am trying to test a static method which involve creating a simple object

 

public void static DummyMethod()

     var obj = new SomeDummy(... ... ...)

 

In the test, I create a real instance of SomeDummy and then let say i changed some value in the fakeInstance e.g. dummyBool = true;

and then called Isolate.Swap.AllInstance<SomeDummy>.With(realObj)

However, I found that the object return after the swap is NOT "realObj" that I want.

 

Is there any way I can do that ?

I need that realObj in my test as I will need the object in Verify WithExactArgument check later on.

 

 

 

Please help.

Thanks,
asked by cscmh99 (6.1k points)

1 Answer

0 votes

Hi,

Isolate.Swap.AllInstance is obsolete.

Use Isolate.Fake.NextInstance OR Isolate.Fake.AllInstances to hook into future object creation.

Both return a proxy object that you can use to verify methods were called.

 

See example:

[TestMethod]
public void Verify_OnFutureInstance_Works()
{
    var fakeFoo = Isolate.Fake.NextInstance<Foo>(Members.CallOriginal, ConstructorWillBe.Called);

    new Foo().Bar();

    Isolate.Verify.WasCalledWithAnyArguments(() => fakeFoo.Bar());
}

 

answered by alex (17k points)
I tried Isolate.Fake.NextInstance and Isolate.Fake.AllInstances

I cannot post code out because of company policy but so I'll have to at least mess up the code a bit in the following

Imagine that I need to test a static method ("StaticFunc") which called another private static method with an argument i.e. ("InnerFunc")

static void StaticFunc()

       newObj = new Foo()

       result = InnerFunc(newObj);

-------------------------------------------------------------------

In Test

I did the Isolate.Fake.AllInstance as you mentioned and tried to mock the return of "InnerFunc" as the following

Isolate.NonPublic.WhenCalled<TestDummy>("InnerFunc").WithExactArguments(fakeFoo).WillReturn(xxxx)

But this never work. Nor the verify withArgument later on.
I don't understand what's to be testederified here.
 
Is it verifying that an object created in your code (newObj) is the exact argument passed to another method?
 
In any case, testing that something happened during execution can be done in verious ways.
 
I've created another example that shows how to do it:
 
public class Foo
{
    internal void DoSomething()
    {
        throw new NotImplementedException();
    }
}

public class UnderTest
{
    private static int result;

    public static void PublicStaticFunc()
    {
        var newObj = new Foo();
        result = privateStaticFunc(newObj);
    }

    private static int privateStaticFunc(Foo foo)
    {
        foo.DoSomething();
        return 0;
    }
}

[TestClass, Isolated]
public class Tests
{
    [TestMethod]
    public void Privates_Statics_AndVerifications()
    {
        var fakeFoo = Isolate.Fake.AllInstances<Foo>();
        Isolate.NonPublic.WhenCalled<UnderTest>("privateStaticFunc")
            .WithExactArguments(fakeFoo)
            .WillReturn(3);

        UnderTest.PublicStaticFunc();

        //Will not work as fakeFoo and newObj is not the same object
        //Isolate.Verify.NonPublic.WasCalled(typeof(UnderTest), "privateStaticFunc").WithArguments(fakeFoo);

        //You can solve this by validating that fake foo was used as expected
        Isolate.Verify.WasCalledWithAnyArguments(()=> fakeFoo.DoSomething());
    }
}
If this is not the case, post the full scenario you're dealing with.
 
It shouldn't include any of your company's code, just the general stracture of this issue + an explenation of what you want to test.
That is exactly the problem I'm having.

The Isolate.NonPublic.WhenCalled mock WithExactArguments is not working.

So, either I can only test the logic WITHOUT verify the arguments

Or, I might need to wrap the create new instance into a wrapper static method so that I can pass fake into the method I guess.

"So, either I can only test the logic WITHOUT verify the arguments"

Why do you want to verify the argument?

Do you want to make sure it's valid?

Is it part of the logic?

Are there many calls to this method and you want to make sure that it was called with a specific arg?

 

If you don't care about the references of the argument to equal, you can override the Equals method of your argument's Type e.g.

 

public class Foo
{
    public override bool Equals(System.Object obj)
    {
        var foo = obj as Foo;
        if (foo == null)
        {
            return false;
        }

        return this.ToString() == foo.ToString();
    }
}

[TestMethod]
public void Privates_Statics_AndVerifications()
{
    //Arrange
    Isolate.NonPublic.WhenCalled<UnderTest>("privateStaticFunc").IgnoreCall();

    var fakeFoo = Isolate.Fake.AllInstances<Foo>();
    Isolate.WhenCalled(() => fakeFoo.Equals(null)).CallOriginal();

    //Act
    UnderTest.PublicStaticFunc();

    //Assert
    Isolate.Verify.NonPublic.WasCalled(typeof(UnderTest), "privateStaticFunc").WithArguments(fakeFoo);
}

Let me know if this helps.

 

...