How to mock IOptionsSnapshot instance for testing

You should be able to mock up the interface and create an instance of the options class for the test. As I am unaware of the nested classes for the options class I am making a broad assumption. Documentation: IOptionsSnapshot //Arrange //Instantiate options and nested classes //making assumptions here about nested types var options = … Read more

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

You are correct: in .NET 4, leaving the APTCA on there makes the assembly SecurityTransparent, and that may be what’s causing you grief. The MSDN article Migrating an APTCA Assembly to the .NET Framework 4 has a good discussion and explanation of the changes to the AllowPartiallyTrustedCallersAttribute in .NET 4. Specifically: The AllowPartiallyTrustedCallers attribute has … Read more

What is the difference between passing It.IsAny() and the value of It.IsAny() to a method setup

It.IsAny only allows Moq to match future invocations of method calls if used within the Setup construct. When Setup is called Moq just adds the method call to its cache of already-set-up method calls. Note that the argument to Setup in your example has type Expression<Func<IFoo, bool>>. Since you are passing in an Expression, the … Read more

How to test method call order with Moq

I recently created Moq.Sequences which provides the ability to check ordering in Moq. You may want to read my post that describes the following: Supports method invocations, property setters and getters. Allows you to specify the number of times a specific call should be expected. Provides loops which allow you to group calls into a … Read more

Use Moq to mock Constructor?

var myMockBOC = new Mock<BusinessObjectContext>(null, null); This will pass nulls in for your two parameters. Another approach would be to create an internal constructor meant for test usage only, and use InternalsVisibleTo to allow your test assembly to use it. Unfortunately this has a big drawback in that if you sign your assemblies, Moq is … 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

Create an Expression using reflection

The code to create the expression dynamically would be like this: ParameterExpression parameter = Expression.Parameter(typeof (GoalsModelUnitOfWork), “i”); MemberExpression property = Expression.Property(parameter, “AcademicCycles”); var queryableType = typeof (IQueryable<>).MakeGenericType(typeof (AcademicCycle)); var delegateType = typeof (Func<,>).MakeGenericType(typeof (GoalsModelUnitOfWork), queryableType); var yourExpression = Expression.Lambda(delegateType, property, parameter); The result will have the desired type, but the problem is that the return … Read more

Moq.Mock – how to set up a method that takes an expression

You can set it up like this: _mockRepos.Setup(x => x.Single<Page>(It.IsAny<Expression<Func<Page, bool>>>()))//.Returns etc…; However you are coming up against one of Moq’s shortcomings. You would want to put an actual expression there instead of using It.IsAny, but Moq doesn’t support setting up methods that take expressions with specific expressions (it’s a difficult feature to implement). The … Read more

Mocking internal classes with Moq for unit testing

You could make internals visible to Moq by adding InternalsVisibleToAttribute in your project’s assembly.cs, like this: [assembly: InternalsVisibleTo(“DynamicProxyGenAssembly2”)] Why “DynamicProxyGenAssembly2” and not “Moq”? It’s the name of dynamic assembly created to contain dynamically generated proxy types (all of this is handled by yet another library, Castle’s DynamicProxy) which is used by Moq. So you expose … Read more