Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, May 29, 2018 10:01 AM
Hi Team,
I have method with return type as "Void" , please tell me how can i write unit test case for these kind of methods.
Method-
public void AddEmployee(object obj)
{
// do something
}
All replies (6)
Tuesday, May 29, 2018 11:14 AM âś…Answered
I think it could be part of infrastructure layer. If you test business layer then you will call AddEmployee method with parameter which is resulting of some part business workflow. You will interest if parameter is object which you expected.
I suspect you have class with AddEmployee method as Mock. Then you can use Callback(object obj) method.
object myObject;
mock.Setup(k=>k.AddEmployee(It.Any<object>())).Callback<object>(k=>myObject = k);
You can test if parameter is as you expected by comparison myObject object with expected object.
Note: Please don't use object as parameter if you don't need it. There should be Employee object as parameter.
Tuesday, May 29, 2018 10:51 AM
Usually when a method does something like "add" they return success which you method does not so you would have to wait until the method finishes then test the outcome in the data source. Since the method is synchronous you would do your assert immediately afterwards.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, May 29, 2018 12:30 PM
So you set up the Moq to return nothing, since the method being mocked has no return value.
Tuesday, May 29, 2018 1:11 PM
yes...do you have any other option, then please let me know.
Tuesday, May 29, 2018 1:28 PM
If you want to test void method there must be something what void method changes. Testing principe is about I have some parameters and there is some result which depends on parameters.
If your method add parameter into list you must test if there are all elements in list. So you need public method to access list items which are compared with objects which you expected. You can have some logic in Add method that inserted object could be changed so you need check your changes.
Tuesday, May 29, 2018 2:31 PM
yes...do you have any other option, then please let me know.
The purpose of mocking a method that has a return value out of the mocked method is to have an object that is useable to other code within the unit of test of the code being tested. Method needs to return a value becuase you did an "Arrange' of the object *the fake' object so that other code that depends upon the object, even if it's fake, so that the unit test will not terminate due to no object being present.
If method returns no value, then the mock should be setup not to Return() anything, the Return() function shouldn't even be used. What's the point? The point is moot, becuase it's a void method.