Home > Enterprise >  c# - Update unit test method with Moq
c# - Update unit test method with Moq

Time:10-28

I am using the Moq framework for my unit test. This is my TestMethod:

[TestFixture]
public class UpdateContactTests
{
    private static readonly object[] TestValidContact =
    {
        new object[]
        {
            "Prueba", "[email protected]", "86456245",
            new LookUpItem()
            {
                LookUpItemId = Guid.NewGuid(), Category = "ContactType", CategoryId = 1, Value = "Bulling Contact"
            },
            new Company()
            {
                Id = Guid.NewGuid(), Name = "Company", Email = "[email protected]", WebSite = "company.com",
                Nda = false, Msa = false, Phone = "84876817"
            }
        }
    };

    [TestCaseSource(nameof(TestValidContact))]

    [Test]
    public void UpdateContact_ValidValues_UpdateContact(string name, string email, string phone, LookUpItem lookUpItem, Company company)
    {
        //arrange
        var id = Guid.NewGuid();
        var data =
            new[]
            {
                new Contact { Id = Guid.NewGuid(), Name = "Test1", Email = "[email protected]", Phone = "86456245",
                    ContactType = 
                        new LookUpItem()
                        {
                            LookUpItemId = Guid.NewGuid(), Category = "ContactType", CategoryId = 1, Value = "Bulling Contact"
                        },
                    Company = 
                        new Company()
                        {
                            Id = Guid.NewGuid(), Name = "Company", Email = "[email protected]", WebSite = "company.com",
                            Nda = false, Msa = false, Phone = "84876817"
                        }},
                new Contact { Id = id, Name = "Test2", Email = "[email protected]", Phone = "86456245",
                    ContactType =
                        new LookUpItem()
                        {
                            LookUpItemId = Guid.NewGuid(), Category = "ContactType", CategoryId = 1, Value = "Bulling Contact"
                        },
                    Company =
                        new Company()
                        {
                            Id = Guid.NewGuid(), Name = "Company", Email = "[email protected]", WebSite = "company.com",
                            Nda = false, Msa = false, Phone = "84876817"
                        }},
            };

        var contact = new Contact()
        {
            Id = id,
            Name = name,
            Email = email,
            Phone = phone,
            ContactType = lookUpItem,
            Company = company
        };

        var _unitOfWork = new Mock<IUnitOfWork>();
        _unitOfWork.Setup(mock => mock.Contact.Get(null, null, null)).Returns(data);
        _unitOfWork.Setup(mock => mock.Company.Get(null, null, null)).Returns(new List<Company>());
        _unitOfWork.Setup(mock => mock.LookUpItem.Get(null, null, null)).Returns(new List<LookUpItem>());
        var contactService = new ContactService(_unitOfWork.Object);

        //act
        contactService.UpdateContact(contact);

        //assert
        Assert.That(data.First(m => m.Id == id).Name, Is.EqualTo(contact.Name));
        _unitOfWork.Verify(mock => mock.Contact.Update(It.IsAny<Contact>()), Times.Once);
    }
}

My problem is that when I run the test a NullReferenceException is thrown, I suppose it is because the list that has the object that I want to modify is not being assigned

I've never really used Moq and I don't know much about unit tests either, so what am I really doing wrong

Edit: i change one of the setup because the one called when the update is done was another instead of

_unitOfWork.Setup(mock => mock.Contact.Get(null, null, null)).Returns(data);

is

_unitOfWork.Setup(mock => mock.Contact.Where(c => c.Id == contact.Id,null,null).FirstOrDefault()).Returns(() => data.Where(c => c.Id == contact.Id));

CodePudding user response:

Without seeing the rest

_unitOfWork.Setup(mock => mock.Contact.Get(null, null, null))

the mock.Contact returns null

best thing is to debug the test, breakpoint the first line and step through to find exactly where it is throwing the null exception

CodePudding user response:

I found the solution I was doing the set like this

_unitOfWork.Setup(mock => mock.Contact.Where(c => c.Id == contact.Id,null,null).FirstOrDefault()).Returns(() => data.Where(c => c.Id == contact.Id));

while in the update method the condition was this

_unitOfWork.Setup(mock => mock.Contact.Where(c => c.Id == contact.Id).FirstOrDefault()).Returns(() => data.Where(c => c.Id == contact.Id));

changing the condition in the method fixed the problem

  • Related