C# IHttpActionResult和集成测试Web API v2放入MS测试
VS2013根据我的EF上下文自动为我生成了一个web api v2控制器。我正在尝试对控制器的put部分进行单元测试。无论我做什么,我都无法让我的断言检查StatusCodeResult返回。自动生成的代码如下所示:C# IHttpActionResult和集成测试Web API v2放入MS测试,c#,asp.net-web-api,mstest,integration-testing,C#,Asp.net Web Api,Mstest,Integration Testing,VS2013根据我的EF上下文自动为我生成了一个web api v2控制器。我正在尝试对控制器的put部分进行单元测试。无论我做什么,我都无法让我的断言检查StatusCodeResult返回。自动生成的代码如下所示: // PUT api/Vendor/5 public IHttpActionResult PutVendor(int id, Vendor vendor) { if (!ModelState.IsValid) {
// PUT api/Vendor/5
public IHttpActionResult PutVendor(int id, Vendor vendor)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != vendor.Id)
{
return BadRequest();
}
db.Entry(vendor).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!VendorExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[TestMethod]
public void PutVendor_UpdateVendorRecord_ReturnsTrue()
{
// Arrange
//CleanUpVendors();
var controller = new VendorController(ctx);
const string vendorName = "Unit Test Company";
// Add vendor to database
ctx.Vendors.Add(new Vendor { Active = true, Name = vendorName });
ctx.SaveChanges();
var myVendor = (from v in ctx.Vendors
where v.Name == vendorName
select v).FirstOrDefault();
// Get Newly Inserted ID
Assert.IsNotNull(myVendor, "Vendor is Null");
myVendor.Name = "New Name";
// Act
var httpActionResult = controller.PutVendor(myVendor.Id, myVendor);
//var response = httpActionResult as OkNegotiatedContentResult<Vendor>;
var response = httpActionResult as OkNegotiatedContentResult<Vendor>;
// Assert
}
我的集成测试如下所示:
// PUT api/Vendor/5
public IHttpActionResult PutVendor(int id, Vendor vendor)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != vendor.Id)
{
return BadRequest();
}
db.Entry(vendor).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!VendorExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[TestMethod]
public void PutVendor_UpdateVendorRecord_ReturnsTrue()
{
// Arrange
//CleanUpVendors();
var controller = new VendorController(ctx);
const string vendorName = "Unit Test Company";
// Add vendor to database
ctx.Vendors.Add(new Vendor { Active = true, Name = vendorName });
ctx.SaveChanges();
var myVendor = (from v in ctx.Vendors
where v.Name == vendorName
select v).FirstOrDefault();
// Get Newly Inserted ID
Assert.IsNotNull(myVendor, "Vendor is Null");
myVendor.Name = "New Name";
// Act
var httpActionResult = controller.PutVendor(myVendor.Id, myVendor);
//var response = httpActionResult as OkNegotiatedContentResult<Vendor>;
var response = httpActionResult as OkNegotiatedContentResult<Vendor>;
// Assert
}
但我不认为它实际上在测试任何东西,除了有某种回报。任何帮助都将不胜感激。考虑到您对正在计划的内存中数据库更改的评论,下面是一些示例,说明如何在您的场景中编写测试:
PeopleController people = new PeopleController();
// mismatched person Id returns BadRequest
Person person = new Person();
person.Id = 11;
person.Name = "John updated";
IHttpActionResult result = people.PutPerson(10, person).Result;
Assert.IsInstanceOfType(result, typeof(BadRequestResult));
// ---------------------------------------------------
// non-existing person
Person person = new Person();
person.Id = 1000;
person.Name = "John updated";
IHttpActionResult result = people.PutPerson(1000, person).Result;
Assert.IsInstanceOfType(result, typeof(NotFoundResult));
// --------------------------------------------------------
//successful update of person information and its verification
Person person = new Person();
person.Id = 10;
person.Name = "John updated";
IHttpActionResult result = people.PutPerson(10, person).Result;
StatusCodeResult statusCodeResult = result as StatusCodeResult;
Assert.IsNotNull(statusCodeResult);
Assert.AreEqual<HttpStatusCode>(HttpStatusCode.NoContent, statusCodeResult.StatusCode);
//retrieve the person to see if the update happened successfully
IHttpActionResult getPersonResult = people.GetPerson(10).Result;
OkNegotiatedContentResult<Person> negotiatedResult = getPersonResult as OkNegotiatedContentResult<Person>;
Assert.IsNotNull(negotiatedResult);
Assert.AreEqual<string>(person.Name, negotiatedResult.Content.Name);
PeopleController people=newpeoplecontroller();
//不匹配的个人Id返回错误请求
Person=新人();
个人识别码=11;
person.Name=“John updated”;
IHttpActionResult=人.PutPerson(10人).result;
IsInstanceOfType(result,typeof(BadRequestResult));
// ---------------------------------------------------
//不存在的人
Person=新人();
个人Id=1000;
person.Name=“John updated”;
IHttpActionResult=人.PutPerson(1000人).result;
Assert.IsInstanceOfType(result,typeof(NotFoundResult));
// --------------------------------------------------------
//成功更新个人信息及其验证
Person=新人();
个人识别码=10;
person.Name=“John updated”;
IHttpActionResult=人.PutPerson(10人).result;
StatusCodeResult StatusCodeResult=结果为StatusCodeResult;
Assert.IsNotNull(statusCodeResult);
Assert.AreEqual(HttpStatusCode.NoContent、statusCodeResult.StatusCode);
//检索此人以查看更新是否成功
IHttpActionResult getPersonResult=people.GetPerson(10.Result);
OkNegotiatedContentResult negotiatedResult=getPersonResult作为OkNegotiatedContentResult;
Assert.IsNotNull(negotiatedResult);
Assert.AreEqual(person.Name、negotiatedResult.Content.Name);
您是否使用真实数据库保存更改?是的,它是通过Entity Framework 6.0.1连接起来的。目前我起诉真实数据库的唯一原因是,我无法使用新版本的EF运行(.NET内存数据库)。一旦工作开始,它将为“单元测试”创建一个内存中的数据库结构,但现在我只是针对一个真正的空sql服务器。努力程度:你是如何获得IHttpActionResult=people.PutPerson(1000,person.result)代码>?我不能直接从控制器方法中获取结果?你的意思是关于.result
…可能我没有明确提及…但我想我是在假设如果操作返回任务
,然后您可以执行.Result
以获得实际结果,或者如果您的单元测试框架支持异步等待
模式,那么您可以避免它……例如,XUnit支持异步等待
。如果您使用ApiController.BadRequest(ModelState)返回结果,那么它将不会转换为BadRequestResult。相反,它将强制转换为InvalidModelStateResult。