gpt4 book ai didi

c# - IHttpActionResult 和集成测试 Web API v2 放入 MS 测试

转载 作者:太空狗 更新时间:2023-10-29 18:18:24 26 4
gpt4 key购买 nike

VS2013 会根据我的 EF 上下文自动为我生成一个 Web API v2 Controller 。我正在尝试对 Controller 的放置部分进行单元测试。无论我做什么,我都无法让我的断言检查 StatusCodeResult 返回值。自动生成的代码如下所示:

 // 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


}

我的测试有问题吗?我的断言应该是什么样的?

这个断言返回真:

Assert.IsInstanceOfType(httpActionResult, typeof(System.Web.Http.Results.StatusCodeResult));

但我不认为它实际上在测试任何东西,除了有某种返回。任何帮助将不胜感激。

最佳答案

考虑到您对计划中的内存数据库更改的评论,以下是您如何在您的场景中进行编写测试的一些示例:

    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);

关于c# - IHttpActionResult 和集成测试 Web API v2 放入 MS 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20380094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com