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 mocking a ChannelFactory<IService> which should return an object that implements IService as well as IChannel.

How do I create a mock which exposes 2 interfaces such that I can use the as keyword.

For example:

var proxy= ChannelFactory<IService>.Create();

var channelOnlyPart = proxy as Channel
asked by Matthew.Relay (680 points)

5 Answers

0 votes
Hi Matthew,

There should be nothing special in faking such object.
consider the next case:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var fakeChannelFactory = Isolate.Fake.Instance<ChannelFactory<Service>>();

            var returnedIService = fakeChannelFactory.Create() as IService;
            var returnedIChannel = fakeChannelFactory.Create() as IChannel;

            Assert.AreEqual(returnedIService, returnedIChannel);
        }
    }


    public class ChannelFactory<T> where T : IService, IChannel, new() 
    {
        public T Create()
        {
            return new T();
        }
    }

    public class Service : IService, IChannel
    {
        
    }

    public interface IService
    {
    }

    public interface IChannel
    {
    }
}



Please let me know if that's what you meant.
answered by alex (17k points)
0 votes
Actually what I wanted is something like this

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var IServiceInterface = Isolate.Fake.Instance<IService>();
            //This is where I am confused.  This is what I want to write
              IServiceInterface.IncludeInterface<IChannel>();
            var fakeChannelFactory = Isolate.Fake.Instance<ChannelFactory<IService>>().Returns(IServiceInterface);



        }
    }



then somewhere in my code, we find

public class GenerateProxyFor<TServiceToCreate> 

private IChannel _openChannel



public     TServiceToCreate CreateService( )
{
     var serviceProxy = _factory.Create();

     _openChannel = serviceProxy as IChannel;

    return serviceProxy;   

}

public void Dispose()
{
   _openChannel.Close();
//Errorhandling and stuff, so I want to be able to mock out _openChannel.State == CommunicationState.Faulted etc. 

}




Let me know if you have any further questions
answered by Matthew.Relay (680 points)
0 votes
Hi,

In this example we have IServiceAndChannel that implements IService and IChannel.
Than we set behavior on fakeFactory.CreateService() and make it return IServiceAndChannel.
"service" is both an IChannel and IService which enables you to use the "as" keyword.

You can do:
service as IChannel;

or:
service as IService ;


        public interface IServiceAndChannel : IService, IChannel
        {
            
        }

        [TestMethod]
        public void TestMethod1()
        {
            var fakeFactory = Isolate.Fake.Instance<ChannelFactory<IService>>();
            Isolate.WhenCalled(() => fakeFactory.CreateService()).WillReturn(Isolate.Fake.Instance<IServiceAndChannel>());
            var service = fakeFactory.CreateService();

            Assert.IsInstanceOfType(service, typeof(IService));
            Assert.IsInstanceOfType(service, typeof(IChannel));
        }
answered by alex (17k points)
0 votes
Hi Matthew,

Please let me know if you find the last example useful.
answered by alex (17k points)
0 votes
I had already something in place that looked like what you recommended.

This was not the ultimate solution, but it is workable.

Matthew
answered by Matthew.Relay (680 points)
...