【发布时间】:2015-03-19 11:45:07
【问题描述】:
我有一个基本的测试用例,它将一个 Delta 对象放在一起,以传递给我能够成功完成的控制器的“PATCH”操作。我的测试代码如下
[TestMethod]
public async Task Patch_Product()
{
// Act
var delta = new Delta<Product>(typeof(Product));
delta.TrySetPropertyValue("Name", "PatchedProduct");
delta.TrySetPropertyValue("Comment", "A test Product that has been patched");
var result = await productController.Patch(1, delta);
// Assert
Assert.IsNotNull(result);
}
当代码到达 Patch 动作的第一行时,如下所示
Validate(patch.GetEntity());
失败并出现以下异常:System.InvalidOperationException: ApiController.Configuration must not be null.
我验证了 ApiController.Configuration 对于我的所有其他测试以及 GET、POST、DELETE 等实际上都为空。但是,这些控制器操作都没有调用此异常所在的“Validate()”方法抛出。有没有人遇到过这个?有没有办法通过潜在地模拟上下文来针对 PATCH 工作进行测试?
我尝试在测试中也传入一个空白配置,如下所示:
productController.Configuration = new HttpConfiguration();
但这似乎也不起作用。我得到了这个例外:
System.IO.FileNotFoundException:无法加载文件或程序集“Newtonsoft.Json,版本=6.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。系统找不到指定的文件。
【问题讨论】:
标签: asp.net-mvc unit-testing controller asp.net-web-api2 patch