How do I unit test a WCF service?

后端 未结 3 1480
走了就别回头了
走了就别回头了 2021-02-05 09:49

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

We\'ve wrapped these DLLs with a thin WCF service layer which our

相关标签:
3条回答
  • 2021-02-05 10:14

    The consumer of your service doesn't care what's underneath your service. To really test your service layer, I think your layer needs to go down to DLLs and the database and write at least CRUD test.

    0 讨论(0)
  • 2021-02-05 10:20

    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.

    For example, in the below service I break out my data access repository using "Poor Man's Dependency Injection".

    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
    

    On the client you can mock the WCF service itself using the interface of the service contract.

    <TestMethod()> _
    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
    
    0 讨论(0)
  • 2021-02-05 10:32

    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.

    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.

    0 讨论(0)
提交回复
热议问题