I have an object (like the HttpContext or other ones) that I would like to mock. Sometimes, there are some unit tests where I'm forced to mock a hefty amount of dependencies, and set their dependencies and values appropriately.
Below there's some sample code to mock the httpcontext and another class:
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var files = new Mock<HttpFileCollectionBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
request.Setup(req => req.ApplicationPath).Returns("~/");
request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
request.Setup(req => req.PathInfo).Returns(string.Empty);
request.Setup(req => req.Form).Returns(new NameValueCollection());
request.Setup(req => req.QueryString).Returns(new NameValueCollection());
request.Setup(req => req.Files).Returns(files.Object);
response.Setup(res => res.ApplyAppPathModifier(MoqIt.IsAny<string>())).
Returns((string virtualPath) => virtualPath);
user.Setup(usr => usr.Identity).Returns(identity.Object);
identity.SetupGet(ident => ident.IsAuthenticated).Returns(true);
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);
return context.Object;
registrationView = new Mock<IRegistrationView>();
registrationView.SetupGet(v => v.Address).Returns("Calle test");
registrationView.SetupGet(v => v.EmailAddress).Returns("[email protected]");
registrationView.SetupGet(v => v.Password).Returns("testpass");
registrationView.SetupGet(v => v.FirstName).Returns("Name");
registrationView.SetupGet(v => v.LastName).Returns("Surname");
registrationView.SetupGet(v => v.DaytimePhoneNumber).Returns("666666666");
registrationView.SetupGet(v => v.WholeSalers).Returns(new List<UserWholesaler>() {
new UserWholesaler(true) { CollaborationCode = "1234", Region = "TestReg", WholesalerCode = "10", WholesalerName = "TestWS", RegionName = "RegTest" } });
I wonder: is there any library that, with simply a call like "registrationView = new MockWithFilledProperties();", would allow me to create a Mock with properties filled with default values? If so, which one?
Thank you and kind regards.
You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.
First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.
Moq allows you to stub all properties with
registrationView.SetupAllProperties();
That will make all properties track their values (i.e. you can write some value to property and retrieve it later) and set all properties to their default values.
NOTE: You can use chained mock creation (aka specification queries) to setup mock more quickly
IRegistrationView view = Mock.Of<IRegistrationView>(ctx =>
ctx.Address == "Calle test" &&
ctx.EmailAddress == "[email protected]" &&
ctx.Password == "testpass" &&
ctx.FirstName == "Name" &&
ctx.LastName == "Surname");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With