Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Get()调用未从单元测试返回值,但API调用在C#中返回值?_C#_Entity Framework_Unit Testing_Asp.net Web Api - Fatal编程技术网

Get()调用未从单元测试返回值,但API调用在C#中返回值?

Get()调用未从单元测试返回值,但API调用在C#中返回值?,c#,entity-framework,unit-testing,asp.net-web-api,C#,Entity Framework,Unit Testing,Asp.net Web Api,使用Test>Debug>All Tests,我可以看到值749作为id从我的TestMethod传递给控制器,但是record.count标识没有找到匹配项。当加载测试时,db(dbcontext)中似乎没有任何信息,而当它正常构建和运行时,所有信息都在那里 我目前正在使用实体框架和Webapi添加一些上下文 使用Postman传递,它返回与749匹配的记录,我还可以看到数据库中的值,这是如何发生的 试验方法 [TestMethod] public void Get_ShouldReturnC

使用Test>Debug>All Tests,我可以看到值749作为id从我的TestMethod传递给控制器,但是record.count标识没有找到匹配项。当加载测试时,db(dbcontext)中似乎没有任何信息,而当它正常构建和运行时,所有信息都在那里

我目前正在使用实体框架和Webapi添加一些上下文

使用Postman传递,它返回与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);
}
[ResponseType(typeof(IQueryable))]
公共IHttpActionResult GetParticipant(内部id)
{
var记录=从n开始,以db.记录为单位
将n.WhoAssignedPlanning.Id等于u.Id的db.Users加入usergp
来自usergp.DefaultIfEmpty()中的u
其中n.ID==ID
选择新参与者到()
{
ID=n.ID,
BobId=n.BobId,
Name=n.Firstname+“”+n.Lastname,
注释,
Firstname=n.Firstname,
Lastname=n.Lastname,
地区,
郊区,
地址,
DOB=n.DOB,
WhoAssignedPlanning=u.FirstName+“”+u.LastName
};
if(records.Count()==0)
{
返回NotFound();
}
返回Ok(记录);
}

您可以右键单击测试方法,然后调试如果您做错了,您必须模拟
dbcontext
尝试查看