chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi guys

I am just trying to get in to using Mocks in my unit testing. I think I understand the basics, but I have a problem I am having problems getting my head around, which I hope someone can help me with.

I'm using a CSLA (http://www.lhotka.net/ArticleIndex.aspx?area=CSLA%20.NET) based architecture for by business logic classes. A feature of this is that it uses class-in-charge constructors to hide data access logic from UI developers. Let me give you an example.

Say I have a customer class I want to load by ID. There is a protected constructor on the class, and then a shared / static factory method to fetch a customer something like this:

 Public Shared Function GetCustomer(ByVal CustomerID As Integer) As Customer
        Return directcast(DataPortal.Fetch(New FetchCriteria(CustomerID)), Customer)
    End Function


What happens in here is that we create a FetchCriteria that is basically just a token to hold the type of class we want and anything specifying what we want to fetch (in this case the customerID). This is then passed to a shared/static dataportal.fetch method. That method then examines the config file to see if we are using any remoting or going straight in to the database. If we are using remoting it fires the request of to a service which basicly does the same remotely as it would locally - which is, use reflection for create an instance of the customer object, and then call a method on it called Dataportal_Fetch. This is a private method (again to hide it from UI developers). The Dataportal_Fetch method then connects to the database, and does all it needs to initialise the object.

So =- perhaps now you can see my problem. I need to test my GetCustomer method, and I also need to test the private Dataportal_Fetch methods - however there is no trivial way to do this unless I go through the Dataportal class and its appropriate static methods - so how can I Mock the Dataportal?

Any advice gratefully accepted!
asked by TimEnsor (600 points)

1 Answer

0 votes
Hi,
I am not sure that I really understand the question but try this:
To test GetCustomer, lets test that:
:arrow: GetCustomer calls DataPortal.Fetch with a correct FetchCriteria:
:arrow: GetCustomer returns the Customer returned by DataPortal.Fetch

<test()> Public Sub TestIt()
    MockManager.Init()
    ' mock DataPortal
    Dim dataPortalMock As Mock = MockManager.MockAll(GetType(DataPortal))
    ' create a dummy customer to return
    Dim expectedCustomer As Customer = SomeMethodThatCreatesACustomer();
    ' return our dummy customer the first time Fetch is called
    ' validate that the correct Creteria is sent
    dataPortalMock.ExpectAndReturn("Fetch",retCustomer).Args(New FetchCriteria(4))
    
    ' Call our method and test that we recived the correct Customer
    Dim retCustomer As Customer = MyClass.GetCustomer(4)
    Assert.AreEqual(expectedCustomer ,retCustomer)

    ' make sure that expected calls where actually made
    MockManager.Verify()
End Sub

The SomeMethodThatCreatesACustomer() can use reflection to create a new Customer.

I hope this helps
answered by scott (32k points)
...