private async Task RegisterCallsignCreate(string imei, string callsign, VehicleType?type, bool shouldCreate = true) { var imeis = CreateMockIMEIDbSet(); var context = CreateMockIMEIContext(imeis.Object); var locationService = CreateMockLocationService(); var service = new IMEIService(context.Object, locationService.Object); await service.RegisterCallsign(imei, callsign, type); if (shouldCreate) { var expectedItem = new IMEIToCallsign { IMEI = imei, CallSign = callsign ?? IMEIService.DefaultCallsign, Type = type ?? VehicleType.Unknown }; imeis.Verify(i => i.Add(It.Is <IMEIToCallsign>(c => c.IMEI == expectedItem.IMEI && c.CallSign == expectedItem.CallSign && c.Type == expectedItem.Type))); context.Verify(c => c.SaveChangesAsync()); } else { imeis.Verify(i => i.Add(It.IsAny <IMEIToCallsign>()), Times.Never); context.Verify(c => c.SaveChangesAsync(), Times.Never); } }
private async Task RegisterCallsignUpdate(string imei, string callsign, VehicleType?type, IMEIToCallsign linkedObject, bool shouldUpdate = true) { var oldValues = new IMEIToCallsign { CallSign = linkedObject.CallSign, Id = linkedObject.Id, IMEI = linkedObject.IMEI, Type = linkedObject.Type }; var imeis = CreateMockIMEIDbSet(); var context = CreateMockIMEIContext(imeis.Object); var locationService = CreateMockLocationService(); var service = new IMEIService(context.Object, locationService.Object); await service.RegisterCallsign(imei, callsign, type); if (string.IsNullOrWhiteSpace(callsign)) { locationService.Verify(l => l.ExpireLocation(It.IsAny <string>()), Times.Never); } else { locationService.Verify(l => l.ExpireLocation(oldValues.CallSign)); } if (shouldUpdate) { Assert.Equal(imei, linkedObject.IMEI); Assert.Equal(string.IsNullOrWhiteSpace(callsign) ? oldValues.CallSign : callsign, linkedObject.CallSign); Assert.Equal(type ?? oldValues.Type, linkedObject.Type); context.Verify(c => c.SaveChangesAsync()); } else { Assert.Equal(imei, linkedObject.IMEI); Assert.Equal(oldValues.CallSign, linkedObject.CallSign); Assert.Equal(oldValues.Type, linkedObject.Type); context.Verify(c => c.SaveChangesAsync(), Times.Never); } }