How to Unit Test HtmlHelper with Moq?

Here’s another article that shows you how to achieve the same thing: public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd) { var mockViewContext = new Mock<ViewContext>( new ControllerContext( new Mock<HttpContextBase>().Object, new RouteData(), new Mock<ControllerBase>().Object), new Mock<IView>().Object, vd, new TempDataDictionary()); var mockViewDataContainer = new Mock<IViewDataContainer>(); mockViewDataContainer.Setup(v => v.ViewData).Returns(vd); return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object); }

Testing controller Action that uses User.Identity.Name

You can use this code public SomeController CreateControllerForUser(string userName) { var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new SomeController(); controller.ControllerContext = mock.Object; return controller; } It uses Moq mocking framework, but sure you can use anything you like.

Moq setting method return value

I think that you need to change the Returns to just return true instead of the lambda. Like this: rabbitConection.Setup(e => e.AddToQueue(null, string.Empty)).Returns(true) EDIT: If this still doesn’t work then it is probably due to the parameters not matching. You are passing in “someExchange” but the mock is set up for string.Empty. If you aren’t … Read more

How do I use Moq and DbFunctions in unit tests to prevent a NotSupportedException?

I know I’m late to the game, but a very simple fix is to write your own method which uses the DbFunction attribute. Then use that function instead of DbFunctions.TruncateTime. [DbFunction(“Edm”, “TruncateTime”)] public static DateTime? TruncateTime(DateTime? dateValue) { return dateValue?.Date; } Using this function will execute the EDM TruncateTime method when used by Linq to … Read more

Unable to Mock HttpClient PostAsync() in unit tests

Non-overridable members (here: HttpClient.PostAsync) may not be used in setup / verification expressions. I also tried to mock the HttpClient the same way you did, and I got the same error message. Solution: Instead of mocking the HttpClient, mock the HttpMessageHandler. Then give the mockHttpMessageHandler.Object to your HttpClient, which you then pass to your product … Read more

Mocking The RouteData Class in System.Web.Routing for MVC applications

RouteData also has a constructor that takes no arguments. Simply create one and add the values to it that you want. No need to mock it when you can create one. var routeData = new RouteData(); routeData.Values.Add( “key1”, “value1” ); var controllerContext = new ControllerContext(httpContextMock.Object, routeData, controllerMock.Object);

How to mock a function call on a concrete object with Moq?

You should use Moq to create your Mock object and set CallBase property to true to use the object behavior. From the Moq documentation: CallBase is defined as “Invoke base class implementation if no expectation overrides the member. This is called “Partial Mock”. It allows to mock certain part of a class without having to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)