Я тестировал один из моих методов в контроллере, и я получал нулевое значение, когда я передавал все значения моего объекта. Я получал AID 0, когда я передавал все значения в объекте.Он возвращает меня в поле «Список», «Заработок», «Информация о члене», «Заработанный ответ». В чем причина?Метод Get имеет следующие репозитории.
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) };
}
}
}
Мой тест состоит в том, чтобы проверить метод в контроллере, который возвращает значение null, передав все значения в метод.Я передаю объект в мой репозиторий, но он показывает ноль
[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
_earningRepository = new Mock<EarningRepository>(context).Object;
_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 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJybWFnZW5jeUBvYnRhaW5jYXJlLmNvbSIsImp0aSI6ImUyMmQwNjJhLWUyNTUtNDlkZS04MmUxLTc1MjgxMTFlOTE1MiIsImlhdCI6MTU2ODgxODQwMCwicm9sIjoiYXBpX2FjY2VzcyIsImVyb2wiOiJTdXBlckFkbWluIiwiaWQiOiI2ODdmMGZjMy1lZjI5LTQ4MGItYTc2Mi0zMGVkZjBlMTkzMWUiLCJBSWQiOiI2IiwibmJmIjoxNTY4ODE4Mzk5LCJleHAiOjE1Njg4MjU1OTksImlzcyI6IndlYkFwaSIsImF1ZCI6Imh0dHBzOi8vZGV2LWFnZW5jeS1hcGktZG90LW9wdGltdW0tb2N0YW5lLTIwMjcxOC5hcHBzcG90LmNvbS8ifQ.lfdMvENhjiSl5ipFXRAw_7aOR6eLT4yLCtbzYu2aZNE"; //Set header
var controllerContext = new ControllerContext()
{
HttpContext = httpContext,
};
_earningController = new EarningController(_earningRepository, _planRepository, _agentRepository, _promotionRepository) { ControllerContext = controllerContext };
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,
};
var MemberInfo = new MemberInfo()
{
AgencyId = 1,
Address1 = "RWP",
City = "Rawalpindi",
Email = "arslanKhanrwp@gmail.com",
Id = 1,
FirstName = "Arslan khan",
ProducerId = 1,
LastName = "Khan",
};
var Plan = new Plan()
{
Id = 1,
BaseAmount = 12,
Name = "Arslan",
Type = 1,
};
var Promotion = new Promotion()
{
Id = 1,
IsActive = true,
Validity = 1,
StartDate = DateTime.Now,
};
var EarningResponse = new EarningResponse()
{
AgencyName = "abc",
MemberId = 1,
AgentEmail = "arslanKhanrwp@gmail.com",
TotalEarning = 1200,
MemberName = "ArslanKhan"
};
context.Plans.AddRange();
context.Earnings.AddRange();
context.SaveChanges();
JsonContentResult result = (JsonContentResult)await _earningController.Get();
//Assert
Assert.AreEqual(0, await context.Contacts.CountAsync());
}
}
}
}