locked
wcf service client testing RRS feed

  • Question

  • Please indulge me with a quick stupid question.  I'm investing heavily in learning testing, which means DI and mocks etc for isolation.  I'm also already a heavy user of WCF.  My question is:

    Given that I see many people complaining on the Internet about the WCF proxy client not being "mockable" and many other articles going to great and complicated lengths to create "mockable" versions of this client for injection into tests that require the service client as a dependency... why is it that people don't just use the service interface that is provided with the proxy object?

    Say I setup 3 default project types... 1 wcf service library, 1 wpf application and 1 test against the wpf application.  Then I add the service reference in the wpf application.  Then I create a test method and inject an IService1 into, say, MainWindowViewModel.  Now in test, I can focus on the functionality of the MainWindowViewModel by creating my own mock such as:

            internal class bogusService1Client : IService1
            {
                #region IService1 Members
    
                public string GetData(int value)
                {
                    throw new NotImplementedException();
                }
    
                public System.Threading.Tasks.Task<string> GetDataAsync(int value)
                {
                    throw new NotImplementedException();
                }
    
                public CompositeType GetDataUsingDataContract(CompositeType composite)
                {
                    throw new NotImplementedException();
                }
    
                public System.Threading.Tasks.Task<CompositeType> GetDataUsingDataContractAsync(CompositeType composite)
                {
                    throw new NotImplementedException();
                }
    
                #endregion
            }

    Now in test can't I just use bogusService1Client instead of new Service1Client()?  Can't I now put whatever junk I want for testing into that class and doesn't it stand in well for the concrete WCFProxyClient?

    I know I'm new to this stuff and I must be missing something, but every time I try to search on how to start WCF testing, I get the nth degree about mocking and ioc frameworks... which I know have their place for many other good reasons, but I don't understand why it is that WCF presents a problem that they must solve... yet I haven't been successful at starting my own WCF test first project so I must not be understand something critical.

    Thank you for the indulgance.

    -Ethan Nelson

    Friday, November 30, 2012 12:45 AM

Answers

  • And this is the answer:

    The Art of Unit Testing: with Examples in .NET

    4.2, the difference between mocks and stubs.  Sure, you can use the IService1 interface to stub out the service... but that only allows you to make assertions about the class under test by guaranteeing inputs.  If you have a true mock wcf proxy client, then you can make assertions about how your class communicated with the proxy itself by asserting against the mock after your class does its work.

    Or something like that... close enough for today.

    • Marked as answer by Ethan Nelson Friday, November 30, 2012 11:15 PM
    Friday, November 30, 2012 11:15 PM

All replies