c# – Moq – 设置HttpResponse

前端之家收集整理的这篇文章主要介绍了c# – Moq – 设置HttpResponse前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用MVC 4,Moq 4和C#4.5为ConrollerContext对象模拟HttpResponse

我的代码在这里:@H_404_3@

var context = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();

context.SetupProperty(c => c.Response = response);

我尝试使用Setup().返回()和SetupGet()但我不断收到以下错误:@H_404_3@

“Property or Indexer ‘System.Web.HttpContextBase.Response’ cannot be assigned to — it is read only.@H_404_3@

我试过谷歌搜索并在这个网站上搜索,但我似乎无法找到答案.@H_404_3@

解决方法

似乎我没有将正确的对象传递给Returns()方法.我应该通过mock的Object属性.

这是正确的代码:@H_404_3@

var context = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();

context.Setup(c => c.Response).Returns(response.Object);

猜你在找的C#相关文章