<p>We have a whole bunch of DLLs that give us access to our database and other applications and services.</p>

<p>We've wrapped these DLLs with a thin WCF service layer which our clients then consume.</p>

<p>I'm a little unsure on how to write unit tests that only test the WCF service layer. Should I just write unit tests for the DLLs, and integration tests for the WCF services? I'd appreciate any wisdom... I know that if my unit tests actually go to the database they won't actually be true unit tests. I also understand that I don't really need to test the WCF service host in a unit test. </p>

<p>So, I'm confused about exactly what to test and how.</p>

[{'answer_id': 37385, 'author': 'Toran Billups', 'author_id': 2701, 'author_profile': 'https://Stackoverflow.com/users/2701', 'pm_score': 3, 'selected': False, 'text': '<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>\n\n<p>For example, in the below service I break out my data access repository using "Poor Man\'s Dependency Injection".</p>\n\n<pre><code>Public Class ProductService\n    Implements IProductService\n\n    Private mRepository As IProductRepository\n\n    Public Sub New()\n        mRepository = New ProductRepository()\n    End Sub\n\n    Public Sub New(ByVal repository As IProductRepository)\n        mRepository = repository\n    End Sub\n\n    Public Function GetProducts() As System.Collections.Generic.List(Of Product) Implements IProductService.GetProducts\n        Return mRepository.GetProducts()\n    End Function\nEnd Class\n</code></pre>\n\n<p>On the client you can mock the WCF service itself using the interface of the service contract.</p>\n\n<pre><code>&lt;TestMethod()&gt; _\nPublic Sub ShouldPopulateProductsListOnViewLoadWhenPostBackIsFalse()\n    mMockery = New MockRepository()\n    mView = DirectCast(mMockery.Stub(Of IProductView)(), IProductView)\n    mProductService = DirectCast(mMockery.DynamicMock(Of IProductService)(), IProductService)\n    mPresenter = New ProductPresenter(mView, mProductService)\n    Dim ProductList As New List(Of Product)()\n    ProductList.Add(New Product)\n    Using mMockery.Record()\n        SetupResult.For(mView.PageIsPostBack).Return(False).Repeat.Once()\n        Expect.Call(mProductService.GetProducts()).Return(ProductList).Repeat.Once()\n    End Using\n    Using mMockery.Playback()\n        mPresenter.OnViewLoad()\n    End Using\n    \'Verify that we hit the service dependency during the method when postback is false\n    Assert.AreEqual(1, mView.Products.Count)\n    mMockery.VerifyAll()\nEnd Sub\n</code></pre>\n'}, {'answer_id': 37387, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 3, 'selected': True, 'text': '<p>The consumer of your service doesn\'t care what\'s underneath your service.\nTo really test your service layer, I think your layer needs to go down to DLLs and the database and write at least <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" rel="nofollow noreferrer">CRUD</a> test. </p>\n'}, {'answer_id': 38316, 'author': 'Jan Soltis', 'author_id': 3997, 'author_profile': 'https://Stackoverflow.com/users/3997', 'pm_score': 3, 'selected': False, 'text': '<p>It depends on what the thin WCF service does. If it\'s really thin and there\'s no interesting code there, don\'t bother unit testing it. Don\'t be afraid to not unit test something if there\'s no real code there. If the test cannot be at least one level simpler then the code under the test, don\'t bother. If the code is dumb, the test will also be dumb. You don\'t want to have more dumb code to maintain.</p>\n\n<p>If you can have tests that go all the way to the db then great! It\'s even better. It\'s not a "true unit test?" Not a problem at all. </p>\n'}]
['https://Stackoverflow.com/questions/37375', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/781/']