How to cope with setting up a complex unit test and ask them only to test the device

I have a method that takes 5 parameters. This method is used to collect the collected information and send it to my server.

I am writing unit test for this method, but I got a little hooked. Some of the parameters are lists of <> classes that require some tweaking for proper tweaking. I have methods that correctly set them in other units (production code units). But if I name them, then I kind of break up the whole idea of ​​unit test (only for one "unit").

So ... what should I do? Am I duplicating the code that installs these objects in my test project (in a helper method), or am I starting to call production code to configure these objects?

Here is a hypothetical example to try to make this clearer:

File: UserDemographics.cs

class UserDemographics
{
     // A bunch of user demographic here
     // and values that get set as a user gets added to a group.
}

File: UserGroups.cs

class UserGroups
{
     // A bunch of variables that change based on 
     //  the demographics of the users put into them.
     public AddUserDemographicsToGroup(UserDemographcis userDemographics)
     {}
}

File: UserSetupEvent.cs

class UserSetupEvent
{
     // An event to record the registering of a user
     // Is highly dependant on UserDemographics and semi dependant on UserGroups
     public SetupUserEvent(List<UserDemographics> userDemographics, 
                           List<UserGroup> userGroups)
     {}
}

file: Communications.cs

class Communications
{
     public SendUserInfoToServer(SendingEvent sendingEvent, 
                                 List<UserDemographics> userDemographics,
                                 List<UserGroup> userGroups, 
                                 List<UserSetupEvent> userSetupEvents)
     {}
}

So, the question is that: unit test SendUserInfoToServerdo I need to duplicate SetupUserEventand AddUserDemographicsToGroupin my test project, or do I just need to call them to help me set up some "real" parameters?

+5
source share
5 answers

You need test duplicates .

You are correct that unit tests should not call other methods, so you need to "fake" the dependencies. This can be done in one of two ways:

  • Manual Duplicates
  • Mocking

.

Moq . unit test "dummy" , :

public class MyTestObject
{
       public List<Thingie> GetTestThingies()
       {
             yield return new Thingie() {id = 1};
             yield return new Thingie() {id = 2};
             yield return new Thingie() {id = 3};
       } 
}

- /, mocks (aka "fakes" ). Mocks - :

Mock<IRepository> repMock = new Mock<IRepository>();
MyPage obj = new MyPage() //let pretend this is ASP.NET
obj.IRepository = repMock.Object;
repMock.Setup(r => r.FindById(1)).Returns(MyTestObject.GetThingies().First());
var thingie = MyPage.GetThingie(1);

Mock Setup , r => r.FindById(1) . expecation. , .

, Moq, , , :

//did we get the instance we expected?
Assert.AreEqual(thingie.Id, MyTestObject.GetThingies().First().Id); 
//was a method called?
repMock.Verify(r => r.FindById(1));

Verify , . .

+1

, ( , ). , , , UserGroups UserDemographics, UserSetupEvent UserGroup, UserDemographics (). List<UserGroup> 'serDemographics, , ?

- , , , unit test. - , :)

0

- , . , ( " " ), Communications.

0

mock-, unit test , , , , , .

0

You can use the NBuilder tool to generate test data. It has a very nice interface and is very easy to use. If your tests should create lists, this works even better. You can read about it here .

0
source

All Articles