i have the following code, which fails during runtime…
var mock = new Mock<ControllerContext>();
mock.SetupGet(x => x.HttpContext.Request
.ServerVariables["HTTP_HOST"]).Returns(domain);
** RunTime Error: Invalid setup on non-overridable
property
I’ve got some code in my controller, which needs to check the domain the user has requested/gone to.
I’m not sure how to mock it up? any ideas?
PS. I’m using the Moq framewoke in the above example .. so I’m not sure if that’s an issue, etc.?
You can’t mock the indexer on a NameValueCollection because it’s not virtual. What I would do is mock the ServerVariables property since that IS virtual. You can fill in your own NameValueCollection. See below
Here’s what I would do: