How to unit test the properties of a variable declared in a test method

I have a service level method that returns an object (Dto) as a parameter.

As part of this method, I create a new business object and pass the values โ€‹โ€‹of the Dto properties to the properties of the business object. Then I pass the business object as a parameter to call the repository method.

How does my unit test ensure that the business object declared in the service test method gets the correct value in its properties?

+4
source share
2 answers

You can make fun of the repository (I hope you can introduce it to the service level) and check if the business object passed to the repository has the expected property values.

EDIT: example


Infrastructure:

public interface IRepository { void Add(BusinessObject item); } public sealed class ServiceLayerContext { private readonly IRepository repository; public ServiceLayerContext(IRepository repository) { this.repository = repository; } public void ProcessDto(IDtoObject dto) { var businessObject = this.CreateBusinessObject(dto); this.repository.Add(businessObject); } private BusinessObject CreateBusinessObject(IDtoObject dto) { } } 

Check pseudo code (because RhinoMocki is not Moq):

  [Test] public void ShouldCreateBusinessOBjectWithPropertiesInitializedByDtoValues() { // ARRANGE // - create mock of the IRepository // - create dto // - setup expectations for the IRepository.Add() method // to check whether all property values are the same like in dto var repositoryMock = MockRepository.GenerateMock<IRepository>(); var dto = new Dto() { ... }; BusinessObject actualBusinessObject = null; repositoryMock.Expect(x => x.Add(null)).IgnoreArguments().WhenCalled( (mi) => { actualBusinessObject = mi[0] as BusinessObject; }).Repeat().Any(); // ACT // - create service layer, pass in repository mock // - execute svc.ProcessDto(dto) var serviceLayerContext = new ServiceLayerContext(repositoryMock); serviceLayerContext.ProcessDto(dto); // ASSERT // - check whether expectations are passed Assert.IsNotNull(actualBusinessObject); Assert.AreEqual(dto.Id, actualBusinessObject.Id); ... } 
+3
source

Many thanks for your help. The code example below uses moq and is written on vb.net for other vb.net programmers who might have similar problems.

Concrete class

 Public Class UserService Implements IUserService Private ReadOnly userRepository As IUserRepository Public Sub New( _ ByVal userRepository As IUserRepository) Me.userRepository = userRepository End Sub Public Sub Edit(userDto As Dtos.UserDto) Implements Core.Interfaces.Services.IUserService.Edit Try ValidateUserProperties(userDto) Dim user = CreateUserObject(userDto) userRepository.Edit(user) Catch ex As Exception Throw End Try End Sub Private Function CreateUserObject(userDto As Dtos.UserDto) As User Implements Core.Interfaces.Services.IUserService.CreateUserObject Dim user = New User With {.Id = userDto.Id, _ .UserName = userDto.UserName, _ .UserPassword = userDto.UserPassword, _ .Profile = New Profile With {.Id = userDto.ProfileId}} Return user End Function Sub ValidateUserProperties(userDto As Dtos.UserDto) End Sub 

Testing class

 <TestFixture()> Public Class UserServiceTest Private userRepository As Mock(Of IUserRepository) Public serviceUnderTest As IUserService <SetUp()> Public Sub SetUp() userRepository = New Mock(Of IUserRepository)(MockBehavior.Strict) serviceUnderTest = New UserService(userRepository.Object) End Sub <Test()> Public Sub Test_Edit() 'Arrange Dim userDto As New UserDto With {.UserName = "gbrown", .UserPassword = "power", .Id = 98, .ProfileId = 1} Dim userObject As User = Nothing userRepository.Setup(Sub(x) x.Edit(It.IsAny(Of User))) _ .Callback(Of User)(Sub(m) userObject = m) 'Act serviceUnderTest.Edit(userDto) 'Assert userRepository.Verify(Sub(x) x.Edit(It.IsAny(Of User)), Times.AtLeastOnce()) Assert.NotNull(userObject) Assert.AreEqual(userDto.Id, userObject.Id) Assert.AreEqual(userDto.ProfileId, userObject.Profile.Id) Assert.AreEqual(userDto.UserName, userObject.UserName) Assert.AreEqual(userDto.UserPassword, userObject.UserPassword) End Sub End Class 
0
source

All Articles