Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to unit test your WCF service classes make sure you design them with loose coupling in mind so you can mock out each dependancy as you only want to test the logic inside the service class itself.</p> <p>For example, in the below service I break out my data access repository using "Poor Man's Dependency Injection".</p> <pre><code>Public Class ProductService Implements IProductService Private mRepository As IProductRepository Public Sub New() mRepository = New ProductRepository() End Sub Public Sub New(ByVal repository As IProductRepository) mRepository = repository End Sub Public Function GetProducts() As System.Collections.Generic.List(Of Product) Implements IProductService.GetProducts Return mRepository.GetProducts() End Function End Class </code></pre> <p>On the client you can mock the WCF service itself using the interface of the service contract.</p> <pre><code>&lt;TestMethod()&gt; _ Public Sub ShouldPopulateProductsListOnViewLoadWhenPostBackIsFalse() mMockery = New MockRepository() mView = DirectCast(mMockery.Stub(Of IProductView)(), IProductView) mProductService = DirectCast(mMockery.DynamicMock(Of IProductService)(), IProductService) mPresenter = New ProductPresenter(mView, mProductService) Dim ProductList As New List(Of Product)() ProductList.Add(New Product) Using mMockery.Record() SetupResult.For(mView.PageIsPostBack).Return(False).Repeat.Once() Expect.Call(mProductService.GetProducts()).Return(ProductList).Repeat.Once() End Using Using mMockery.Playback() mPresenter.OnViewLoad() End Using 'Verify that we hit the service dependency during the method when postback is false Assert.AreEqual(1, mView.Products.Count) mMockery.VerifyAll() End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload