测试Patch OData WebAPI方法 [英] Testing the Patch odata webapi method
本文介绍了测试Patch OData WebAPI方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的测试项目的odata控制器中测试以下Patch方法.
I need to test the following Patch method in my odata controller from my test project.
[ValidateModel]
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<User> patch)
{
var user = await db.Users.FindAsync(key);
if (user == null)
{
return NotFound();
}
patch.Patch(user);
Validate(user);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
db.Entry(user).Property(p => p.UserType).IsModified = false;
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(key))
{
return NotFound();
}
throw;
}
return Updated(user);
}
测试项目中的代码如下.有人可以告诉我如何将值传递给Delta参数.目前,我在line controller.Patch(1,user);上遇到编译错误.
The code in the test project is as follows. Could someone tell me how do I pass value to the Delta parameter. Currently I am getting compilation errors at line controller.Patch(1, user);.
[TestMethod]
public void TestPatch()
{
// Arrange
var controller = new UsersController();
var user = new User();
user.Id = 1;
user.Lastname = "Johanson";
// Act
controller.Patch(1, <System.Web.OData.Delta> user);
// Assert
}
推荐答案
var delta = new Delta<User>(typeof(User));
delta.TrySetPropertyValue("Id", 1);
delta.TrySetPropertyValue("Lastname", "Johanson");
我不知道是否有任何帮助者可以简化这一过程
I don't know if there are any helpers to make that easier
这篇关于测试Patch OData WebAPI方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文