Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<Sam.Alyza.WcfInterface.Website> rc = null;
- Service<Sam.Alyza.WcfInterface.IServiceWebsites>.Use(alyzaSvc =>
- {
- rc = new List<Sam.Alyza.WcfInterface.Website>(alyzaSvc.GetSites());
- });
- List<Sam.Alyza.WcfInterface.Website> rc = new List<Sam.Alyza.WcfInterface.Website>(_datamodel.GetSites());
- public delegate void UseServiceDelegate<T>(T proxy);
- public static class Service<T>
- {
- public static ChannelFactory<T> _channelFactory;
- public static void Use(UseServiceDelegate<T> codeBlock)
- {
- if (_channelFactory == null)
- _channelFactory = new ChannelFactory<T>("AlyzaServiceEndpoint");
- IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
- bool success = false;
- try
- {
- codeBlock((T)proxy);
- proxy.Close();
- success = true;
- }
- finally
- {
- if (!success)
- {
- proxy.Abort();
- }
- }
- }
- }
- Service<Sam.Alyza.WcfInterface.IServiceWebsites>.Use(alyzaSvc =>
- {
- rc = new List<Sam.Alyza.WcfInterface.Website>(alyzaSvc.GetSites());
- });
- public class MockWebsiteService : WcfInterface.IServiceWebsites
- {
- internal List<Sam.Alyza.WcfInterface.Website> _websites = new List<Sam.Alyza.WcfInterface.Website>();
- internal int _GetSitesCallCount;
- IEnumerable<Sam.Alyza.WcfInterface.Website> Sam.Alyza.WcfInterface.IServiceWebsites.GetSites()
- {
- _GetSitesCallCount++;
- return _websites;
- }
- }
- public static class Service<T>
- {
- #if DEBUG
- public static T DebugOverride = default(T);
- #endif
- public static ChannelFactory<T> _channelFactory;
- public static void Use(UseServiceDelegate<T> codeBlock)
- {
- #if DEBUG
- if (!Object.Equals(DebugOverride, default(T)))
- {
- codeBlock(DebugOverride);
- return;
- }
- #endif
- if (_channelFactory == null)
- _channelFactory = new ChannelFactory<T>("AlyzaServiceEndpoint");
- IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
- bool success = false;
- try
- {
- codeBlock((T)proxy);
- proxy.Close();
- success = true;
- }
- finally
- {
- if (!success)
- {
- proxy.Abort();
- }
- }
- }
- }
- MockWebsiteService mockmodel = new MockWebsiteService();
- Service<WcfInterface.IServiceWebsites>.DebugOverride = mockmodel;
- // run my tests here
- interface IClientFactory
- {
- TClient CreateClient<TClient>();
- }
- class ClientFactory : IClientFactory
- {
- TClient CreateClient<TClient>()
- {
- var channelFactory = new ChannelFactory<TClient>("AlyzaServiceEndpoint");
- var proxy = (TClient)channelFactory.CreateChannel();
- return proxy;
- }
- }
- public ViewModel
- {
- public ViewModel(IClientFactory clientFactory)
- {
- _clientFactory = clientFactory;
- }
- private void DoWcfStuff()
- {
- using (var proxy = _clientFactory.CreateClient<IClientChannel>())
- {
- var result = proxy.GetThings();
- }
- }
- }
- public ViewModelTests
- {
- public void Setup()
- {
- _mockFactory = new MockClientFactory();
- _viewModel = new ViewModel(_mockFactory);
- }
- [Test]
- public void Test()
- {
- var testResult = new Result();
- var mockClient = _mockFactory.CreateClient<IClientChannel>();
- mockClient.SetResultForGetThings(testResult);
- // put the viewmodel through its paces.
- }
- private class MockClientFactory : IClientFactory
- {
- MockClient _mockClient;
- public MockClientFactory()
- {
- _mockClient = new MockClient();
- }
- public TClient CreateClient<TClient>()
- {
- if (typeof(TClient) == typeof(IClientChannel))
- {
- return _mockClient;
- }
- }
- }
- private class MockClient : IClientChannel
- {
- void SetupGetThingsResult(Result result)
- {
- _result = result;
- }
- Result GetThings()
- {
- return _result;
- }
- }
- }
Add Comment
Please, Sign In to add comment