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 swap an instance of proxy class to service.
My normal calling to this proxy is with "Using" statment like this:
using (proxy = new ProxyClass())
{
    proxy.CallService();
}


because of this class is disposable and getting dispose after call I'm getting a null reference exception.

the test should verify that this proxy was called.

Thanks
Adiel
asked by adiel.sharabi (3.4k points)

1 Answer

0 votes
Can you post a more complete example of the issue you've encountered?

I've tried running the following code and it worked fine:
[TestMethod]
public void TestMethod1()
{
    var fakeProxy = Isolate.Fake.Instance<ProxyClass>();
    Isolate.WhenCalled(() => fakeProxy.CallService()).IgnoreCall();
    Isolate.Swap.NextInstance<ProxyClass>().With(fakeProxy);


    var classUnderTest = new SomeClass();
    classUnderTest.RunAction();

    Isolate.Verify.WasCalledWithAnyArguments(() => fakeProxy.CallService());
}

public class SomeClass
{
    public void RunAction()
    {
        using (var proxy = new ProxyClass())
        {
            proxy.CallService();
        }
    }
}

public class ProxyClass : IDisposable
{
    public void Dispose()
    {

    }

    public void CallService()
    {
    }
}
answered by dhelper (11.9k points)
...