Home > database >  How to write Nunit test for a method which passes an object (C#)
How to write Nunit test for a method which passes an object (C#)

Time:11-20

I am quite new to Nunit test and trying to figure out writing tests for a particular method. As can be seen below, I have got this method name AddorGetCustomer which takes a parameter, Order. I have attached how the Order looks like after this method.

public class Class : IClass
    {
        private readonly CRMEntities _db;
        private readonly IMapper _mapper;
        private readonly IService _service;

        public CustomerService(CRMEntities db, IMapper mapper, IService service)
        {
            _db = db;
            _mapper = mapper;
            _service = service;
        }

        public Data.Customer AddorGetCustomer(Order order)
        {
            if (order.BillToAddress == null) return null;
            
            var address = _mapper.Map<CustomerAddress>(order.BillToAddress);
            if (string.IsNullOrEmpty(address.Email)) return null;

            return AddOrGet(address, order.Branch, order.OrderNumber, order.ShipmentNumber, order.OrderDate);
        }

The Order that I am passing into, looks something like:

        public string  Name { get; set; }

        public decimal Country { get; set; }

        public Address BillToAddress { get; set; }

Address:

public class Address
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Company { get; set; }

        public string Address1 { get; set; }

        public string Address2 { get; set; }

        public string Address3 { get; set; }

        public string Address4 { get; set; }

        public string PostalCode { get; set; }

        public string CountryCode { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }
    }

So,

  1. I want check when (order.BillToAddress is null), then my test should return null,

  2. when (address.Email) is null or empty, return null. How could I do it? After hours of googling, couldnt find a way of writing test in Nunit. Any hints or suggestion would be appreciated (Keeping in mind that I am very new to Nunit)

That's what I have done so far: 1.

 public class CustomerServiceTests
    {
        private Mock<CRMEntities> _moqCRMEntities;
        private CustomerService _customerService;
        private IMapper _mapper;
        private Order _order;
        private Address _address;

        [SetUp]

        public void Setup()
        {
            _moqCRMEntities = new Mock<CRMEntities>();
            _address = new Address()
            {
                FirstName = "X",
                LastName = "Y",
                Company = "MW",
                Address1 = "28 Street",
                Address2 = "29 Street",
                Address3 = "5 Road",
                Address4 = "10 London Road",
                PostalCode = "IT6 U7G",
                CountryCode = "90",
                Phone = "07785365",
                Email = "[email protected]"
            };
            _order = new Order()
            {
                BillToAddress = _address
            };
            
        }

        [Test]
        public void Whe()
        {
            _customerService = new CustomerService(null, _mapper, _loyaltyServiceMock.Object);
            //Act 
            var result = _customerService.AddorGetCustomer(_order);
            //Assert
            Assert.AreEqual(_order,result);
        }
    }
  1. [Test] public void EmailIsNullOrEmpty_AddorGetCustomer_ReturnsNull() { var address = new Address { FirstName = "Jerry" };

              var order = new Order
              {
                  Branch = "MW",
                  OrderNumber = 1,
                  BillToAddress = address
              };
              //Act 
              var result = _customerService.AddorGetCustomer(order);
              //Assert
              Assert.Null(result);
          }
    

CodePudding user response:

    [Test]
    public void AddorGetCustomer_When_Order_BillToAddressIsNull_ReturnsNull()
    {
        var order = new Order
        {
            Name = "foo",
            Country = 1m;
            BillToAddress = null
        }

        _customerService = new CustomerService(null, _mapper, _loyaltyServiceMock.Object);

        //Act 
        var result = _customerService.AddorGetCustomer(order);

        //Assert
        Assert.Null(result);
    }
  • Related