Mock nested dependencies with MOQ - c#

I assumed that MOQ would automatically create Mocks for any nested dependencies.
I am unit testing an ASP.Net MVC Controller:
public class TransactionController : Controller
{
private readonly ITransactionService _transactionService;
private readonly SearchPanelVmBuilder _searchPanelVmBuilder;
private readonly TransactionVmsBuilder _transactionVmsBuilder;
public TransactionController(TransactionVmsBuilder transactionVmsBuilder, ITransactionService transactionService, SearchPanelVmBuilder searchPanelVmBuilder)
{
_transactionVmsBuilder = transactionVmsBuilder;
_transactionService = transactionService;
_searchPanelVmBuilder = searchPanelVmBuilder;
}
// other methods omitted for brevity
public PartialViewResult SearchPanel()
{
var vm = _searchPanelVmBuilder.BuildVm();
return PartialView("_SearchPanel", vm);
}
}
The unit test code:
[Fact]
public void SeachPanel_Calls_BuildSearchPanelVm()
{
// Arrange
var mockTransService = new Mock<ITransactionService>();
var mockTransVmsBuilder = new Mock<TransactionVmsBuilder>();
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>();
var controller = new TransactionController(mockTransVmsBuilder.Object, mockTransService.Object, mockSearchPanelVmBuilder.Object);
// Act
controller.SearchPanel();
// Assert
mockSearchPanelVmBuilder.Verify(x => x.BuildVm());
}
MOQ complains:
Can not instantiate proxy of class: MCIP.Web.UI.ViewModelBuilders.Singular.SearchPanelVmBuilder.
Could not find a parameterless constructor.
The class it can't instantiate a proxy for:
public class SearchPanelVmBuilder
{
private readonly ITransactionTypeService _transactionTypeService;
private readonly TransactionTypeVmBuilder _transactionTypeVmBuilder;
private readonly UserProvider _userProvider;
public SearchPanelVmBuilder(
UserProvider userProvider,
ITransactionTypeService transactionTypeService,
TransactionTypeVmBuilder transactionTypeVmBuilder
)
{
_userProvider = userProvider;
_transactionTypeService = transactionTypeService;
_transactionTypeVmBuilder = transactionTypeVmBuilder;
}
public virtual SearchPanelVm BuildVm()
{
return new SearchPanelVm
{
Userlist = _userProvider.GetOperators(),
TransactionTypes =
_transactionTypeService.GetAll().Select(x => _transactionTypeVmBuilder.BuildVmFromModel(x)).ToList()
};
}
}
Its corresponding dependencies:
public class UserProvider
{
private static int retryCount;
public virtual List<string> GetOperators()...
public virtual List<string> GetGroupsForUser(WindowsIdentity identity)...
}
public interface ITransactionTypeService
{
List<TransactionType> GetAll();
}
public class TransactionTypeVmBuilder
{
public virtual TransactionTypeVm BuildVmFromModel(TransactionType transactionType)...
}
Am I doing something wrong?
Do I have to explicitly tell MOQ to have a go at auto-mocking nested dependencies?
Or do i have to explicitly set up the nested Mocks - like this:
var mockUserProvider = new Mock<UserProvider>();
var mockTransTypeService = new Mock<ITransactionTypeService>();
var mockTransactionTypeVmBuilder = new Mock<TransactionTypeVmBuilder>();
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>(mockUserProvider.Object, mockTransTypeService.Object, mockTransactionTypeVmBuilder.Object);

Yes your assumption is correct. Because the class SearchPanelVmBuilder haven't provided parameterless constructor Mock for this class can't be created like this:
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>()
Causes exception: Could not find a parameterless constructor.
Instead create the Mock by providing all the parameters, something like this:
UserProvider userProvider = new UserProvider();
Mock<ITransactionTypeService> transactionTypeService = new Mock<ITransactionTypeService>();
TransactionTypeVmBuilder transactionTypeVmBuilder = new TransactionTypeVmBuilder();
// Use constructor with parameters here because SearchPanelVmBuilder
// doesn't have parameterless constructor
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>(
userProvider, transactionTypeService.Object, transactionTypeVmBuilder);
var mockTransService = new Mock<ITransactionService>();
var mockTransVmsBuilder = new Mock<TransactionVmsBuilder>();
var controller = new TransactionController(
mockTransVmsBuilder.Object,
mockTransService.Object,
mockSearchPanelVmBuilder.Object);
Then the instance of the controller TransactionController can be created and the method SearchPanel can be called on it.

In this case, all you want to test is that BuildVM is called and the result is returned so you don't need to mock the inner dependencies. You do need to setup a return value for the BuildVM method in the Arrange section before you call the SearchPanel method.
mockSearchPanelVmBuilder.Setup(x => x.BuildVM()).Returns(mockSearchPanelVM);
Because your mocking a class and virtual method if you don't setup a return value the actual implementation will be run. With an interface an error will be thrown that indicated you should setup a return value.

Related

Fake IMongoQueryable with FakeItEasy

I'm developing an API which communicates with MongoDB and I need to create some statistics from one collection. I have the following service:
public class BoxService : IBoxService
{
private readonly IMongoCollection<Box> _boxCollection;
public BoxService(IOptions<DbSettings> dbSettings, IMongoClient mongoClient)
{
var mongoDatabase = mongoClient.GetDatabase(dbSettings.Value.DatabaseName);
_boxCollection = mongoDatabase.GetCollection<Box>(dbSettings.Value.BoxCollectionName);
}
public async Task<List<BoxStatisticsDto>> GetBoxNumberStatisticsAsync()
{
var boxNumberStatisticsList = new List<BoxStatisticsDto>();
var results = await _boxCollection.AsQueryable()
.Select(box => new { box.WarehouseId, Content = box.Content ?? string.Empty })
.ToListAsync();
// More calculations with the results list
return boxNumberStatisticsList;
}
}
And the following test:
public class BoxServiceTest
{
private readonly IMongoCollection<Box> _boxCollection;
private readonly List<Box> _boxes;
private readonly IBoxService _boxService;
public BoxServiceTest()
{
_boxCollection = A.Fake<IMongoCollection<Box>>();
_boxes = new List<Box> {...};
var mockOptions = A.Fake<IOptions<DbSettings>>();
var mongoClient = A.Fake<IMongoClient>();
var mongoDb = A.Fake<IMongoDatabase>();
A.CallTo(() => mongoClient.GetDatabase(A<string>._, default)).Returns(mongoDb);
A.CallTo(() => mongoDb.GetCollection<Box>(A<string>._, default)).Returns(_boxCollection);
_boxService = new BoxService(mockOptions, mongoClient);
}
}
This is working so far, the BoxService is created with the fake parameters and I can test other functionalities of the service (FindAll, FindById, Create, etc.) but how can I test the GetBoxNumberStatisticsAsync function? I can't fake the AsQueryable because it's an extension method.
As you've noted, you can't fake an extension method. This question is asked every once in a while. For example, see Faking an Extension Method in a 3rd Party Library. There are a few approaches:
if the static method is simple enough, to divine what it does and fake the non-static methods that it calls
add a layer of indirection: wrap the call to the extension method in an interface that you can fake
don't fake the database. Instead, replace it with some in-memory analogue, if one exists (I don't know what's available for Mongo)
Here is what I ended up with. A base interface for all my services:
public interface IBaseService<T>
{
//generic method definitions for all services: Findall, FindById, Create, Update
}
An abstract class to have a generic constructor:
public abstract class BaseService<T> : IBaseService<T>
{
protected BaseService(IOptions<DbSettings> dbSettings, IMongoClient mongoClient, string collectionName)
{
var mongoDatabase = mongoClient.GetDatabase(dbSettings.Value.DatabaseName);
Collection = mongoDatabase.GetCollection<T>(collectionName);
}
protected IMongoCollection<T> Collection { get; }
// abstract method definitions for IBaseService stuff
public virtual async Task<List<T>> CollectionToListAsync()
{
return await Collection.AsQueryable().ToListAsync();
}
}
An interface for my BoxService:
public interface IBoxService : IBaseService<Box>
{
public Task<List<BoxStatisticsDto>> GetBoxNumberStatisticsAsync();
}
The service itself:
public class BoxService : BaseService<Box>, IBoxService
{
public BoxService(IOptions<DbSettings> dbSettings, IMongoClient mongoClient)
: base(dbSettings, mongoClient, dbSettings.Value.BoxCollectionName)
{
}
public async Task<List<BoxStatisticsDto>> GetBoxNumberStatisticsAsync()
{
var boxNumberStatisticsList = new List<BoxStatisticsDto>();
var list = await CollectionToListAsync();
var results = list.Select(box => new { box.WarehouseId, Content = box.Content ?? string.Empty }).ToList();
//...
return boxNumberStatisticsList;
}
}
And finally the test:
public async Task GetBoxNumberStatisticsAsync_ReturnsStatistics()
{
// Arrange
var expected = new List<BoxStatisticsDto> {...};
var fakeService = A.Fake<BoxService>(options => options.CallsBaseMethods());
A.CallTo(() => fakeService.CollectionToListAsync()).Returns(_boxes);
// Act
var boxList = await ((IBoxService)fakeService).GetBoxNumberStatisticsAsync();
// Assert
}
I'm not a huge fan of making the CollectionToListAsync public, but nothing really worked for me here. I tried creating IQueryable and IEnumerable from my list and convert them to IMongoQueryable but no success. I also tried faking the IMongoQueryable but I couldn't execute the Select on it as it gave an error that the 'collectionNamespace' can't be null and the CollectionNamespace can't be faked, because it's a sealed class.

How to make the ActivatorUtilities.CreateInstance method use a different constructor?

Is there a way the tell the ActivatorUtilities.CreateInstance<T>(IServiceProvider serviceProvider); method to try to use other constructors if the first one can't be constructed?
I have a class with multiple constructors:
public ViewModelB(SomeDependency someDependency): this one only takes SomeDependency which is registered in a DI container
public ViewModelB(SomeDependency someDependency, GetUserRequest request): this one takes SomeDependency which is registered in a DI container and a GetUserRequest which has to be passed in manually
And I'm trying to activate them and resolve dependencies like so:
IServiceProvider serviceProvider; //this gets passed from somewhere
Guid userId; //this gets passed manually by the caller
//works
var instanceAWithoutParams = ActivatorUtilities.CreateInstance<ViewModelA>(serviceProvider);
//works
var instanceAWithParams = ActivatorUtilities.CreateInstance<ViewModelA>(serviceProvider, new[] { new GetUserRequest { UserId = userId } });
//does NOT work, it tries to use the first constructor and fails
var instanceBWithoutParams = ActivatorUtilities.CreateInstance<ViewModelB>(serviceProvider);
//works
var instanceBWithParams = ActivatorUtilities.CreateInstance<ViewModelB>(serviceProvider,, new[] { new GetUserRequest { UserId = userId } });
The activation of instanceBWithoutParams fails because it can't resolve the request parameter. It tries to use the first constructor and doesn't check other ones when the activation fails.
Here's what the services look like, they're the same with one difference: the order of the constructors.
public class ViewModelA
{
private readonly SomeDependency _someDependency;
private readonly GetUserRequest? _request;
public ViewModelA(SomeDependency someDependency)
{
_someDependency = someDependency;
}
public ViewModelA(SomeDependency someDependency, GetUserRequest request)
{
_someDependency = someDependency;
_request = request;
}
}
public class ViewModelB
{
private readonly SomeDependency _someDependency;
private readonly GetUserRequest? _request;
public ViewModelB(SomeDependency someDependency, GetUserRequest request)
{
_someDependency = someDependency;
_request = request;
}
public ViewModelB(SomeDependency someDependency)
{
_someDependency = someDependency;
}
}
public class GetUserRequest
{
public Guid UserId { get; set; }
}
Thanks.
I struggled with the same issue. Eventually I came up with this solution:
I would use something like a factory which is able to construct ServiceB by calling a method.
For example:
var serviceBFactory = ActivatorUtilities.CreateInstance<ServiceBFactory>(serviceProvider);
var instanceBWithoutParams = serviceBFactory.CreateServiceB();
var instanceBWithParams = serviceBFactory.CreateServiceB(new Request());
This way you keep you DI clean. But this means that the ServiceBFactory need to know which services need to be injected in a ServiceB. (so that will be a tight coupling) They come as a package.
I've chosen to re-design the view models instead of trying to pass optional parameters next to services from DI (thanks to Steven for the helpful articles: 1 and 2).
There also seems to be no way of making the ActivatorUtilities.CreateInstance<T>(IServiceProvider serviceProvider); method try other constructors after one fails, so here's what my edited solution looks like.
I've moved the initialization of the optional parameter out of the constructor, that way I only have one constructor that only takes injectables. The parameter is then passed separately via the TakeParameter method. The only downside I can think of is that the parameter can no longer be readonly and I can live with that.
My custom activator utility:
public interface IAcceptParameter<T>
{
void TakeParameter(T parameter);
}
public static class CustomActivator
{
public static T CreateInstance<T>()
{
return ActivatorUtilities.CreateInstance<T>(_serviceProvider);
}
public static T CreateInstanceWithParam<T, K>(K parameter) where T : IAcceptParameter<K>
{
var instance = ActivatorUtilities.CreateInstance<T>(_serviceProvider);
instance.TakeParameter(parameter);
return instance;
}
}
Changed view model
public class SomeViewModel : IAcceptParameter<Guid>
{
private readonly SomeDependency _someDependency;
private Guid? _userId;
public SomeViewModel(SomeDependency someDependency)
{
_someDependency = someDependency;
}
public void TakeParameter(Guid parameter){
_userId = parameter;
}
}
How I use it
var instanceWithoutParam = CustomActivator.CreateInstance<SomeViewModel>(serviceProvider);
Guid userId;
var instanceWithParam = CustomActivator.CreateInstanceWithParam<SomeViewModel, Guid>(serviceProvider, userId);
Let say you have a class like this:
public class a
{
public string p { get; set; }
public a()
{
p = "default constructor";
}
public a(string pv)
{
p = pv;
}
}
You can use .GetConstructor method to use a specific constructor:
public class Program
{
static void Main(string[] args)
{
var c = typeof(a).GetConstructor(new Type[] { typeof(string) });
if (c != null)
{
var myA = (a)c.Invoke(new object[] { "new value" });
Console.WriteLine($"Value of p is {myA.p}");
}
}
}

How to create mock for method that is called using object instance in MOQ C#?

I created Test for a method that is being tested using MOQ & NUnit. The method to be tested will will another method using an object of that corresponding class. When I try to mock that called method, I am not able to invoke the mocked method. How to mock this method, because my testing method is using the other said method. Please help me on this.
public DataSet ExecuteCondition()
{
var ObjClass1 = new Class1();
....
var result = ObjClass1.VerifyPrecondition(query);
....
}
public class Class1:IClass1
{
public string VerifyPrecondition(string query)
{
....
return text;
}
}
So, I suppose this should look like this:
Class with ExecuteCondition() method:
public class DataClass
{
private readonly IClass1 _class1;
public DataClass(IClass1 class1)
{
_class1 = class1;
}
public DataSet ExecuteCondition()
{
//....
var result = _class1.VerifyPrecondition(query);
//....
}
}
Test:
[Test]
public void Test()
{
var mockClass1 = new Mock<IClass1>();
mockClass1.Setup(x => x.VerifyPrecondition(It.IsAny<string>())).Returns("test");
var dataClass = new DataClass(mockClass1.Object);
dataClass.ExecuteCondition();
//Assert
}

Override Autofixture customization setup

I've a class with several services injected in its constructor. I'm using Autofixture with xUnit.net and NSubstitute, and created an attribute to setup the global customization.
public class AutoDbDataAttribute : AutoDataAttribute
{
public AutoDbDataAttribute() : base(() => new Fixture().Customize(new AutoNSubstituteCustomization()))
{
}
public AutoDbDataAttribute(Type customizationType) : base(() =>
{
var customization = Activator.CreateInstance(customizationType) as ICustomization;
var fixture = new Fixture();
fixture.Customize(new AutoNSubstituteCustomization());
fixture.Customize(customization);
return fixture;
})
{
}
}
I also have a custom customization class that setups the common customization for the test methods in the same class.
public class RevenueProviderCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Register<IRevenueContextService>(() =>
{
var contextService = Substitute.For<IRevenueContextService>();
contextService.GetContext().Returns(fixture.Create<RevenueContext>());
return contextService;
});
fixture.Register<ICompanyService>(() =>
{
var companyService = Substitute.For<ICompanyService>();
companyService.Get(Arg.Any<Guid>()).Returns(fixture.Create<Company>());
return companyService;
});
}
}
Now, some of my tests depend on modifying specific properties in the objects returned by the services. So in some cases, I want to modify the RevenueContext and in some cases, I want to modify the Company data.
What I did was creating another object inside the test itself and modify the Returns of the service with the new object, like this:
[Theory]
[AutoDbData(typeof(RevenueProviderCustomization))]
public void ShouldReturnCompanyRevenue(RevenueProvider sut, Company company, [Frozen]IRevenueContextService contextService)
{
var fixture = new Fixture();
RevenueContext context = fixture.Build<RevenueContext>().With(c => c.DepartmentId, null).Create();
contextService.GetContext().Returns(context);
sut.GetRevenue().Should().Be(company.Revenue);
}
But this doesn't work. The RevenueContext from the RevenueProviderCustomization is still used.
Does anyone know how I can override the return from the service? I don't want to setup the fixture one by one in my test, so I was hoping to be able to create a 'general setup' and modify as needed according to the test case.
UPDATE 1
Trying the answer from Mark, I changed the test to
[Theory]
[AutoDbData(typeof(RevenueProviderCustomization))]
public void ShouldReturnCompanyRevenue([Frozen]IRevenueContextService contextService, [Frozen]Company company, RevenueProvider sut, RevenueContext context)
{
context.DepartmentId = null;
contextService.GetContext().Returns(context);
sut.GetRevenue().Should().Be(company.Revenue);
}
The problem is because the RevenueContext is called in the RevenueProvider constructor. So my modification to the DepartmentId happens after the call was made.
public RevenueProvider(IRevenueContextService contextService, ICompanyService companyService)
{
_contextService = contextService;
_companyService = companyService;
_company = GetCompany();
}
public double GetRevenue()
{
if (_hasDepartmentContext)
return _company.Departments.Single(d => d.Id == _departmentId).Revenue;
else
return _company.Revenue;
}
private Company GetCompany()
{
RevenueContext context = _contextService.GetContext();
if (context.DepartmentId.HasValue)
{
_hasDepartmentContext = true;
_departmentId = context.DepartmentId.Value;
}
return _companyService.Get(context.CompanyId);
}
Assuming that RevenueProvider essentially looks like this:
public class RevenueProvider
{
private readonly ICompanyService companySvc;
public RevenueProvider(ICompanyService companySvc)
{
this.companySvc = companySvc;
}
public object GetRevenue()
{
var company = this.companySvc.Get(Guid.Empty);
return company.Revenue;
}
}
Then the following test passes:
[Theory]
[AutoDbData(typeof(RevenueProviderCustomization))]
public void ShouldReturnCompanyRevenue(
[Frozen]ICompanyService companySvc,
RevenueProvider sut,
Company company)
{
companySvc.Get(Arg.Any<Guid>()).Returns(company);
var actual = sut.GetRevenue();
Assert.Equal(company.Revenue, actual);
}
This scenario is exactly what the [Frozen] attribute is designed to handle. The various attributes that AutoFixture defines are applied in the order of the arguments. This is by design, because it enables you to pull out a few values from the argument list before you freeze a type.
In the OP, [Frozen] is only applied after sut, which is the reason the configuration of the mock doesn't apply within the SUT.

Unit testing a method that uses (dynamic) using Moq

I have the below method:
public void Enqueue(ICommand itemToQueue)
{
if (itemToQueue == null)
{
throw new ArgumentNullException("itemToQueue");
}
// Using the dynamic keywork to ensure the type passed in to the generic
// method is the implementation type; not the interface.
QueueStorage.AddToQueue((dynamic)itemToQueue);
}
With QueueStorage being a dependency that implements IQueueStorage. I wish to unit test it but the (dynamic) keyword seems to be blocking Moq from binding correctly to it. The keyword is used to correctly assign the concrete class type rather than ICommand interface type when it is added to the queue.
The unit test looks like this:
[Test]
public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
{
int timesAddToQueueCalled = 0;
var dummyQueueStorage = new Mock<IQueueStorage>();
var testCommand = new TestCommand();
var queueManager = new AzureCommandQueueManager();
dummyQueueStorage
.Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
.Callback(() => timesAddToQueueCalled++);
queueManager.QueueStorage = dummyQueueStorage.Object;
queueManager.Enqueue(testCommand);
Assert.AreEqual(1, timesAddToQueueCalled);
}
Whilst test command is a blank implementation of ICommand:
private class TestCommand : ICommand
{
}
public interface ICommand
{
}
The timesAddedToQueuCalled is not being incremented. I've tried using It.IsAny<ICommand> and (testCommand) to no avail. It looks like the Callback method is not being executed. Can anyone see what I'm doing wrong?
EDIT: IQueueStorage code:
public interface IQueueStorage
{
void AddToQueue<T>(T item) where T : class;
T ReadFromQueue<T>() where T : class;
}
Here is code which works without problems:
public class AzureCommandQueueManager
{
public void Enqueue(ICommand itemToQueue)
{
if (itemToQueue == null)
throw new ArgumentNullException("itemToQueue");
QueueStorage.AddToQueue((dynamic)itemToQueue);
}
public IQueueStorage QueueStorage { get; set; }
}
public interface IQueueStorage
{
void AddToQueue<T>(T command) where T : class;
}
public class TestCommand : ICommand {}
public interface ICommand {}
And test method:
[Test]
public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
{
int timesAddToQueueCalled = 0;
var dummyQueueStorage = new Mock<IQueueStorage>();
var testCommand = new TestCommand();
var queueManager = new AzureCommandQueueManager();
dummyQueueStorage
.Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
.Callback(() => timesAddToQueueCalled++);
queueManager.QueueStorage = dummyQueueStorage.Object;
queueManager.Enqueue(testCommand);
Assert.AreEqual(1, timesAddToQueueCalled);
}
The only difference I see - you have private modifier of TestCommand class. Btw if it is private, how do you access that class from your tests?

Categories