Untitled
unknown
plain_text
8 months ago
2.2 kB
5
Indexable
Never
using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Microsoft.Xrm.Sdk; using System.Collections.Generic; // Assuming your original method is in a class named MyDataService [TestClass] public class MyDataServiceTests { [TestMethod] public void RetrieveContactFromSystemUser_ShouldReturnContactForValidSystemUserId() { // Arrange Guid validSystemUserId = Guid.NewGuid(); var serviceMock = new Mock<IOrganizationService>(); // Setup a mock response for the RetrieveMultiple method var contactEntity = new Entity("contact"); contactEntity.Attributes["fullname"] = "John Doe"; contactEntity.Attributes["contactid"] = Guid.NewGuid(); var entityCollection = new EntityCollection(new List<Entity> { contactEntity }); serviceMock.Setup(service => service.RetrieveMultiple(It.IsAny<QueryExpression>())) .Returns(entityCollection); var myDataService = new MyDataService(); // Create an instance of your service // Act var result = myDataService.RetrieveContactFromSystemUser(serviceMock.Object, validSystemUserId); // Assert Assert.IsNotNull(result); Assert.AreEqual("John Doe", result.GetAttributeValue<string>("fullname")); // Add more assertions based on your specific requirements and expected data } [TestMethod] public void RetrieveContactFromSystemUser_ShouldReturnNullForInvalidSystemUserId() { // Arrange Guid invalidSystemUserId = Guid.NewGuid(); var serviceMock = new Mock<IOrganizationService>(); // Setup a mock response for the RetrieveMultiple method (empty collection) serviceMock.Setup(service => service.RetrieveMultiple(It.IsAny<QueryExpression>())) .Returns(new EntityCollection()); var myDataService = new MyDataService(); // Create an instance of your service // Act var result = myDataService.RetrieveContactFromSystemUser(serviceMock.Object, invalidSystemUserId); // Assert Assert.IsNull(result); } }
Leave a Comment