Get()调用不是从单元测试返回值,而是在C#中进行API调用?

时间:2017-04-11 06:52:13

标签: c# entity-framework unit-testing asp.net-web-api

使用测试>调试>所有测试我可以看到值749作为从我的TestMethod到Controller的id传递,但record.count确定没有找到匹配项。加载测试时,db(dbcontext)似乎没有信息,而当它构建并正常运行时,它就在那里。

我目前正在使用Entity Framework和Webapi添加一些上下文。

使用Postman我传递http://localhost:54047/api/Records/GetParticipant/749并返回匹配749的记录,我也可以看到数据库中的值,这怎么会发生?

测试方法

[TestMethod]
public void Get_ShouldReturnCorrectParticipant()
{
    var testParticipants = GetTestParticipants(); //Returns List
    RecordsController controller = new RecordsController();
    var result = controller.GetParticipant(749);
    Assert.IsNotNull(result);
}

RecordController.cs

[ResponseType(typeof(IQueryable<ParticipantDTO>))]
public IHttpActionResult GetParticipant(int id)
{
    var records = from n in db.Records
            join u in db.Users on n.WhoAssignedPlanning.Id equals u.Id into usergp
            from u in usergp.DefaultIfEmpty()
            where n.ID == id
            select new ParticipantDTO()
            {
                ID = n.ID,
                BobId = n.BobId,
                Name = n.Firstname + " " + n.Lastname,
                Notes = n.Notes,
                Firstname = n.Firstname,
                Lastname = n.Lastname,
                Region = n.Region,
                Suburb = n.Suburb,
                Address = n.Address,
                DOB = n.DOB,
                WhoAssignedPlanning = u.FirstName + " " + u.LastName
            };
    if (records.Count() == 0)
    {
        return NotFound();
    }

    return Ok(records);
}

0 个答案:

没有答案