How to write this EF Mock setup code as a reusable Generic Boilerplate?

Tim Larson already offered a great solution for this boilerplate code in his blog: public static class DbSetMocking { private static Mock<DbSet<T>> CreateMockSet<T>(IQueryable<T> data) where T : class { var queryableData = data.AsQueryable(); var mockSet = new Mock<DbSet<T>>(); mockSet.As<IQueryable<T>>().Setup(m => m.Provider) .Returns(queryableData.Provider); mockSet.As<IQueryable<T>>().Setup(m => m.Expression) .Returns(queryableData.Expression); mockSet.As<IQueryable<T>>().Setup(m => m.ElementType) .Returns(queryableData.ElementType); mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()) .Returns(queryableData.GetEnumerator()); return … Read more

how do I mock sqlconnection or should I refactor the code?

SqlBulkWriter class is tightly coupled to implementation concerns which make it difficult to test the class in isolation. Knowing the connection string is not really a concern of that class and can be delegated out to another service. Something like public interface IDbConnectionFactory { IDbConnection CreateConnection(); } and its implementation for your class would look … Read more

Moq & Interop Types: works in VS2012, fails in VS2010?

Edit : It works for me when I try it in Visual Studio 2012 and target .Net 4.0, only using the .Net PIA’s not the COM ref. Same solution doesn’t work in VS2010. VS2010 loads version’s 10.0.30319.1 of the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll’s and VS2012 loads version’s 11.0.50727.1. You can see the different version’s in the Modules window. … Read more

Mocking using Moq in c#

Classic example which demonstrates that if you cannot unit test a particular component, REFACTOR it! This is why I love what any mocking framework enforces you to do – write decoupled code. In your example, the ProductBusiness class is tightly coupled with the ProductDataAccess class. You could decouple it using (like most of the answers … Read more

How to throw a SqlException when needed for mocking and unit testing?

I have a solution to this. I’m not sure whether it’s genius or madness. The following code will create a new SqlException: public SqlException MakeSqlException() { SqlException exception = null; try { SqlConnection conn = new SqlConnection(@”Data Source=.;Database=GUARANTEED_TO_FAIL;Connection Timeout=1″); conn.Open(); } catch(SqlException ex) { exception = ex; } return(exception); } which you can then use … Read more

How to mock Controller.User using moq

You need to Mock the ControllerContext, HttpContextBase and finally IPrincipal to mock the user property on Controller. Using Moq (v2) something along the following lines should work. [TestMethod] public void HomeControllerReturnsIndexViewWhenUserIsAdmin() { var homeController = new HomeController(); var userMock = new Mock<IPrincipal>(); userMock.Expect(p => p.IsInRole(“admin”)).Returns(true); var contextMock = new Mock<HttpContextBase>(); contextMock.ExpectGet(ctx => ctx.User) .Returns(userMock.Object); var … Read more