繁体   English   中英

我无法读取我刚刚在对象中设置的属性。 为什么?

[英]I cannot read a property that I just set in my object. Why?

我正在使用起订量。 这个例子很简单。 设置属性后,我无法读取属性。

var mock = new Mock<HttpResponseBase>();
mock.Setup(x => x.Cookies).Returns(new HttpCookieCollection());
mock.Setup(x => x.Headers).Returns(new NameValueCollection());
var response = mock.Object;

// now I set StatusCode
response.StatusCode = 404;

// now I try to read, but StatusCode is 0 here, not 404
var statusCode = response.StatusCode

如您所见,我没有为StatusCode指定任何特殊行为。 我该怎么做才能让我写和读它?

这是因为,为了使一个属性像普通属性一样,您应该在其上调用SetupProperty方法。

mock.SetupProperty(x => x.StatusCode);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM