lundi 30 septembre 2019

Testing a method which return me Null?

was Testing On One of my method In Controller And i was getting null while i am passing all the value of my object I was Getting AID 0 while i am passing all the values in object. It returns me Null on List Plan,Earning ,Member Info,Earning Response What is the Reason?? Get Method have following Repositories.

public class EarningController : BaseApiController
{
    private readonly IEarningRepository _earningRepository;
    private readonly IPlanRepository _planRepository;
    private readonly IAgentRepository _agentRepository;
    private readonly IPromotionRepository _promotionRepository;

    public EarningController(IEarningRepository earningRepository, IPlanRepository planRepository,
        IAgentRepository agentRepository, IPromotionRepository promotionRepository)
    {
        _earningRepository = earningRepository;
        _planRepository = planRepository;
        _agentRepository = agentRepository;
        _promotionRepository = promotionRepository;
    }

    [HttpGet("Get")]
    public async Task<ActionResult> Get()
    {
        List<Earning> earnings = await _earningRepository.GetProducerEarnings(AId);
        List<Plan> plans = await _planRepository.GetAllAsync();
        List<Promotion> promotions = await _promotionRepository.GetAll();
        //pending refer a friend
        List<MemberInfo> members = await _agentRepository.GetMembers(AId);
        List<EarningResponse> earningResponses = new List<EarningResponse>();

        foreach (var member in members)
        {
            var memberPlan = plans.FirstOrDefault(x => x.Id == member.SubscriptionId);

            if (memberPlan != null)
                member.Subscription = memberPlan;

            var e = earnings.Where(x => x.MemberId == member.Id).ToList();
            if (e.Count > 0)
                earningResponses.Add(new EarningResponse(member, e, promotions));
        }

        ReportResponse reportResponses = new ReportResponse(earningResponses);

        return new JsonContentResult { StatusCode = (int)HttpStatusCode.OK, Content = JsonSerializer.SerializeObject(reportResponses) };
    }
}

Here Is My Test Method Which i was Testing

[TestClass]
public class EarningControllerTest
{
    EarningController _earningController;
    EarningRepository _earningRepository;
    PlanRepository _planRepository;
    AgentRepository _agentRepository;
    PromotionRepository _promotionRepository;

    DbContextOptionsBuilder<AgencyPortalDbContext> builder = new DbContextOptionsBuilder<AgencyPortalDbContext>()
               .EnableSensitiveDataLogging()
               .UseInMemoryDatabase(Guid.NewGuid().ToString());


    [TestMethod]
    public async Task Get()
    {

        using (var context = new AgencyPortalDbContext(builder.Options))
        {
            //Arrange
            var _earningRepository = new Mock<EarningRepository>(context);
            _planRepository = new Mock<PlanRepository>(context).Object;
            _agentRepository = new Mock<AgentRepository>(context).Object;
            _promotionRepository = new Mock<PromotionRepository>(context).Object;

            var httpContext = new DefaultHttpContext();
            httpContext.Request.Headers["token"] = "Bearer  //Set header
            var controllerContext = new ControllerContext()
            {
                HttpContext = httpContext,
            };
            _earningController = new EarningController(_earningRepository.Object, _planRepository, _agentRepository, _promotionRepository) { ControllerContext = controllerContext };

            //var Earning = new Earning()
            //{
            //    Id = 1,
            //    Agent = 1,

            //};


            var Earning = new Earning()
            {
                Agent = new Agent
                {
                    Address1 = "London",
                    AccountNumber = "1234567765432",
                    AgencyId = 1,
                    Id = 1,
                    City = "Rwp",
                    ContactNumber = "5555344",
                    LastName = "Khan",
                    Email = "arslanKhanrwp@gmail.com",
                    FirstName = "Arslan",
                },
                Id = 1,
                Amount = 200,
                Date = DateTime.Now,
            };
           _earningRepository.Setup( x => x.GetProducerEarnings(Earning.Id)Returns(new List<Earning>()
            {
                new Earning
                {
                Id = 1, MemberId = 1,
                }
            }

                ); 
             _earningRepository.Setup(x => x.GetProducerEarnings(It.IsAny<int>())).Callback(() => {
                 new List<Earning>()
            {
                new Earning
                {
                    Id = 1,
                    Agent = new Agent { AgencyId=1 }

                     }
                 };
             });

    Assert.AreEqual(0, await context.Contacts.CountAsync());
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire