RhinoMocks - Unable to cast type foo to type bar when using non static Repository - unit-testing

I am trying out RhinoMocks (only just started, please be gentle!) and am confused as to why if I use this setup:
var mockRepos = new MockRepository();
var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>(null);
var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>(null);
mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);
var someSampleClass = new SomeClassOrOther(mockServiceProvider);
At some point in SomeClassOrOther, the method GetService is called
var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
and causes the exception:
Unable to cast object of type 'Castle.Proxies.ObjectProxyd0bf4b879a6341bbba3478cf1189d621' to type 'Microsoft.Xrm.Sdk.IPluginExecutionContext'.
However if I use:
var mockServiceProvider = MockRepository.GenerateMock<IServiceProvider>(null);
var mockContext = MockRepository.GenerateMock<IPluginExecutionContext>(null);
mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);
i.e. the static MockRepository.GenerateMock and it executes with no errors.
Where am I going wrong here?

I don't know why you are getting this very exception. However, when using the "old" non-static syntax, you get mocks in "Record" state. Before using them, you need to set them in Replay mode:
var mockRepos = new MockRepository();
var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>();
var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>();
mockRepos.ReplayAll();
mockServiceProvider
.Stub(x => x.GetService(typeof(IPluginExecutionContext)))
.Return(mockContext);
var someSampleClass = new SomeClassOrOther(mockServiceProvider);

You are mixing new AAA syntax and old Record/Replay syntax. When you are using Expect extension method and new AAA syntax, you don't need to operate on mocks repository and change its mode manually(Record/Replay). Create mocks with new static methods (which create mocks in replay mode) and verify expectations later:
// Arrange
var mockServiceProvider = MockRepository.GenerateMock<IServiceProvider>();
var mockContext = MockRepository.GenerateMock<IPluginExecutionContext>();
mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext)))
.Return(mockContext);
var someSampleClass = new SomeClassOrOther(mockServiceProvider);
// Act
// Assert
mockServiceProvider.VerifyAllExpectations();
Here is old Record/Replay syntax for comparison:
var mockRepos = new MockRepository();
var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>();
var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>();
Expect.Call(mockServiceProvider.GetService(typeof(IPluginExecutionContext)))
.Return(mockContext);
mockRepos.ReplayAll();
var someSampleClass = new SomeClassOrOther(mockServiceProvider);
// exercise your SUT
mockRepos.VerifyAll();

Related

How to create Masstransit's Test Harness with Automock?

In order to create the test harness for the SubmitOrderConsumer, I will have to build a ServiceCollection containing all the depdencies of SubmitOrderConsumer.
Mock<INotifier> mockNotifier = new Mock<INotifier>();
Mock<IStockChecker> mockStockChecker = new Mock<IStockChecker>();
Mock<IFidelityUpdater> mockFidelityUpdater = new Mock<IFidelityUpdater>();
Mock<IPromotionManager> mockPromotionManager = new Mock<IPromotionManager>();
await using var provider = new ServiceCollection()
.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<SubmitOrderConsumer>();
})
.AddSingleton<INotifier>(mockNotifier.Object)
.AddSingleton<IStockChecker>(mockStockChecker.Object)
.AddSingleton<IFidelityUpdater>(mockFidelityUpdater.Object)
.AddSingleton<IPromotionManager>(mockPromotionManager.Object)
..
.BuildServiceProvider(true);ds
var harness = provider.GetRequiredService<ITestHarness>();
...
mockNotifier.Verify(...)
The Moq.Automock library helps me to create the SubmitOrderConsumer and auto-mock all its depdencies so the above codes is reduced to
AutoMocker mocker = new AutoMocker();
await using var provider = new ServiceCollection()
.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<SubmitOrderConsumer>();
})
.AddSingleton(mocker.CreateInstance<SubmitOrderConsumer>())
.BuildServiceProvider(true);
var harness = provider.GetRequiredService<ITestHarness>();
...
mocker.GetMock<INotifier>().Verify(...)
Question:
The class AutoMocker is also a IServiceProvider, so the above codes have 2 IServiceProvider:
the first IServiceProvider is the mocker which can resolve the SubmitOrderConsumer
the second IServiceProvider is the provider which can resolve the ITestHarness
is it possible to create the ITestHarness with my mocker IServiceProvider? something like
ITestHarness harness = new ContainerTestHarness(mocker);

XUnit: How to assert exception thrown by the controller in ASP.NET Core

I have a method inside the controller, which does some checks when the check is not true, it will throw an error. How I will catch that error thrown by the API with the specific message?
I`m confused about how to make those types of assertions.
This is my code so far, but I didn't manage to catch the error thrown by the controller. What I'm doing wrong?
[Fact]
public async Task AppendEmailBase64Dto_InvalidBase64_ReturnBadRequest()
{
// Arrange
var emailControllerMocks = new EmailControllerMocks();
var mockLogger = emailControllerMocks.MockLogger();
var mockMapper = emailControllerMocks.MockMapper();
var mockEmsWorkUnit = emailControllerMocks.MockEmsWorkUnit();
var mockAzureBlob = emailControllerMocks.MockAzureBlobAndGetTemplate();
// Setup
var userRequestTemplateString = File.ReadAllText(#".\EmailController\UserRequestTemplate.txt");
mockAzureBlob.Setup(blob => blob.GetHtmlBlob(It.IsAny<string>(), It.IsAny<Uri>()))
.ReturnsAsync(userRequestTemplateString);
// Act
var emailController = new Controllers.ApiV10.EmailController(mockLogger.Object, mockMapper.Object, mockEmsWorkUnit.Object, mockAzureBlob.Object);
var jsonString = File.ReadAllText(#".\EmailController\TemplateBase64Invalid.json");
var testEmailBase64Dto = GeneralHelpers.Deserialize<EmailBase64Dto>(jsonString);
var badRequestResult = await emailController.AppendEmailBase64Dto(testEmailBase64Dto);
//var result = badRequestResult.Value as ObjectResult;
//var errorMessage = badRequestResult.Value as List<string>;
//var statusCode = badRequestResult.StatusCode;
// Assert
//Assert.Equal("The provided base64 template is not a valid base64 string", errorMessage[0]);
//Assert.Equal(400, statusCode);
}
Use Assert.ThrowsAsync (the asynchronous counterpart of Assert.Throws) to assert that an exception of a particular type is thrown.
In your case you'll want something like:
var ex = await Assert.ThrowsAsync<HttpException>(async () => await emailController.AppendEmailBase64Dto(testEmailBase64Dto));
Obviously adjust 'HttpException' to whatever type of exception you're expecting.
You're not going to be able to check the return type in the same test (because AppendEmailBase64Dto will not return when an exception is thrown), so you'll need to test the exception in a separate test.

Getting error as 'Expected invocation on the mock at least once, but was never performed' after executing unit test

I have below code which navigates from one page to other using Navigation Service in Xamarin forms.
On clicking on Observation button it executes ObservationsCommand as shown below.
public Command ObservationsCommand => new Command(async () => await OnObservationsCommandAsync());
After clicking on Observation button it navigates to next page passing the selected data to the navigation service as shown
private async Task OnObservationsCommandAsync()
{
ObservationDetailsParameter selectedData = new ObservationDetailsParameter
{
Cage = DisplayedCage,
Dossiers = DossierList.SelectedItems
};
await _navigationService.NavigateToAsync<ObservationDetailsViewModel>(selectedData);
}
Below is the code for unit test
[Fact]
public void TestOnObservationsCommandAsync()
{
var mockNavigationService = new Mock<INavigationService>();
var mockCageDetailsService = new MockCageDetailsService();
var mockObservationDetailsService = new MockObservationDetailsService();
var mockSettingsService = new MockSettingsService();
Cage _displayedCage = new Cage { Id = 11 };
Dossier _dossier1 = new Dossier { Id = 841 };
var _dossierList = new SelectableItemCollection<Dossier>
{
_dossier1
};
_dossierList.SelectAll();
var cageObsViewModel = new CageObsViewModel(mockNavigationService.Object, mockCageDetailsService, mockSettingsService);
var mockObservationDetailsParameter = new
Mock<IObservationDetailsParameter>();
mockObservationDetailsParameter.Setup(x =>
x.Cage).Returns(_displayedCage);
mockObservationDetailsParameter.Setup(x => x.Dossiers
).Returns(_dossierList.SelectedItems );
cageObsViewModel.DisplayedCage = mockObservationDetailsParameter .Object .Cage ;
cageObsViewModel.DossierList = _dossierList;
// Act
cageObsViewModel.ObservationsCommand.Execute(null);
mockNavigationService.Verify((s) => s.NavigateToAsync<ObservationDetailsViewModel>(mockObservationDetailsParameter.Object));
}
However after executing this test i get error as 'Expected invocation on the mock at least once, but was never performed.'
Will you please help?
This is an example of a unit test I have to verify navigation after successful login.
[Fact]
public async Task ValidCredentials()
{
//Mock login -> false
var authenticationServiceMock = new Mock<IAuthenticationService>();
authenticationServiceMock
.Setup(s => s.Login(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult(true));
LoginViewModel viewModel = new LoginViewModel(authenticationServiceMock.Object);
await viewModel.InitializeAsync(null);
viewModel.UserName.Value = "johann#mail.com";
viewModel.Password.Value = "1234!Maaaa";
viewModel.LoginCommand.Execute(null);
navigationServiceMock.Verify((s) => s.NavigateToAsync<HomeViewModel>());
}
As I am using AutoFac and my ViewModelBase gets the NavigationService instance by itself this is my test setup:
public LoginViewModelTests()
{
ContainerBuilder builder = new ContainerBuilder();
navigationServiceMock = new Mock<INavigationService>();
navigationServiceMock.SetReturnsDefault<Task>(Task.FromResult(0));
builder.RegisterInstance<INavigationService>(navigationServiceMock.Object);
ViewModelLocator.RegisterDependencies(builder);
}
If it is not clear enough let me know and we can review it to better adapt to your case.

How to write test using xUnit for service layer?

How do I write a test using xUnit for a service layer? I'm using dependency injection for my service layer, but how can I make an instance of it using an in memory database?
Here is what I got so far: I follow this link and here is what I came up to use my service layer, but it was a mess and I don't know how to simplify it. I am using an interface of the service in all of my controllers.
[Fact]
public void Add_writes_to_database()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
.Options;
// Run the test against one instance of the context
using (var context = new ApplicationDbContext(options))
{
var productRepo = new Repository<Product>(context);
var categoryRepo = new Repository<Category>(context);
var categoryMappingRepo = new Repository<ProductCategoryMapping>(context);
var categoryService = new CategoryService(context, categoryRepo, categoryMappingRepo);
var manufacturerRepo = new Repository<Manufacturer>(context);
var manufacturerMappingRepo = new Repository<ProductManufacturerMapping>(context);
var manufacturerService = new ManufacturerService(context, manufacturerRepo, manufacturerMappingRepo);
var imageRepo = new Repository<Image>(context);
var imageMappingRepo = new Repository<ProductImageMapping>(context);
var imageService = new ImageManagerService(imageRepo, imageMappingRepo);
var specificationRepo = new Repository<Specification>(context);
var specificationMappingRepo = new Repository<ProductSpecificationMapping>(context);
var specificationService = new SpecificationService(context, specificationRepo, specificationMappingRepo);
var productService = new ProductService(context, productRepo, categoryService, manufacturerService, imageService, specificationService);
var product = new Product() { Id = Guid.NewGuid(), Name = "Product1", Price = 100m };
productService.InsertProduct(product);
}
// Use a separate instance of the context to verify correct data was saved to database
using (var context = new ApplicationDbContext(options))
{
var productRepo = new Repository<Product>(context);
var categoryRepo = new Repository<Category>(context);
var categoryMappingRepo = new Repository<ProductCategoryMapping>(context);
var categoryService = new CategoryService(context, categoryRepo, categoryMappingRepo);
var manufacturerRepo = new Repository<Manufacturer>(context);
var manufacturerMappingRepo = new Repository<ProductManufacturerMapping>(context);
var manufacturerService = new ManufacturerService(context, manufacturerRepo, manufacturerMappingRepo);
var imageRepo = new Repository<Image>(context);
var imageMappingRepo = new Repository<ProductImageMapping>(context);
var imageService = new ImageManagerService(imageRepo, imageMappingRepo);
var specificationRepo = new Repository<Specification>(context);
var specificationMappingRepo = new Repository<ProductSpecificationMapping>(context);
var specificationService = new SpecificationService(context, specificationRepo, specificationMappingRepo);
var productService = new ProductService(context, productRepo, categoryService, manufacturerService, imageService, specificationService);
Assert.Equal(1, productService.GetAllProduct().Count());
}
}
My productService has lots of dependencies to other services, repository and context.
Yes, you have a lot of dependent services and repositories. Since you already use Dependency Injection for your services, I suggest that you make use of an IoC Container. This will not only remove lots of code to setup your integration tests, it will also help you to resolve any service in your application easily.
You could create a class for your type mappings like this:
public class Services
{
private readonly DbContextOptions _options;
private readonly IUnityContainer _container;
public Services(DbContextOptions options)
{
_options = options;
_container = new UnityContainer();
RegisterTypes();
}
private void RegisterTypes()
{
_container.RegisterType<IApplicationDbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager(), new InjectionConstructor(_options));
_container.RegisterType<IProductService, ProductService>(new ContainerControlledLifetimeManager());
_container.RegisterType<ISpecificationService, SpecificationService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IImageManagerService, ImageManagerService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IRepository<ProductSpecificationMapping>, Repository<ProductSpecificationMapping>>(new ContainerControlledLifetimeManager());
// etc ...
}
public T Get<T>()
{
return _container.Resolve<T>();
}
}
Then you are able to minimize the code in your test which is needed to resolve a product service:
[Fact]
public void Add_writes_to_database()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
.Options;
var services = new Services(options);
var target = services.Get<IProductService>();
// to your testing
}
I haven't tested and verified these lines of code in VS, but it should give you an idea. We are using this approach with Unity in one of our application as well, and you can use your favorite IoC Container. You also need Interfaces for your Repositories and Services, but it is better to have them anyway :-)

Moq - Setup method with complex parameters

I'm trying to set up a mock of this interface:
public interface IAuthenticatedRequestService
{
HttpClient CreateHttpClientForJwt(Func<HttpResponseMessage, bool> isUnauthenticated, int timeoutSeconds);
HttpClient CreateHttpClientForAccessToken(Func<HttpResponseMessage, bool> isUnauthenticated, int timeoutSeconds);
}
This is one implementation of the method to setup that is in use and working:
public HttpClient CreateHttpClientForAccessToken(Func<HttpResponseMessage, bool> isUnauthenticated, int timeoutSeconds)
{
var client = Mvx.Resolve<IPlatformOperationProvider>().CreateHttpClient(timeoutSeconds);
return new HttpClient(new AuthenticatedHttpMessageHandler(this, client, AuthenticationUtils.AddAccessTokenToRequest, isUnauthenticated,_loggingService));
}
This is one usage of the implemented method that is working:
var client = service.CreateHttpClientForAccessToken(x => x.StatusCode == HttpStatusCode.Unauthorized, CoreConstants.TimeoutMyDetails);
This is my unit test which sets up the mock:
[Test]
public async void TestIsLoggedInIsTrue()
{
//Arrange
var authenticatedRequestService = new Mock<IAuthenticatedRequestService>();
authenticatedRequestService.Setup(foo => foo.CreateHttpClientForAccessToken((It.IsAny<Func <HttpResponseMessage, bool>>())
, 0
)).Returns(new HttpClient());
var platformOperationProvider = new Mock<IPlatformOperationProvider>();
platformOperationProvider.Setup(foo => foo.CreateHttpClient(1)).Returns(new HttpClient());
Mvx.RegisterSingleton<IPlatformOperationProvider>(platformOperationProvider.Object);
Mvx.RegisterSingleton<IAuthenticatedRequestService>(authenticatedRequestService.Object);
var loggedInProvider = new LoggedInProvider(
new Mock<ISecuredSettings>().Object,
new Mock<ILoggingService>().Object
);
//Act
await loggedInProvider.SetUserAndToken(
new User(),
new ApiAccessInfo("refresh token", "access token", "jwt")
);
//Assert
Assert.IsTrue(loggedInProvider.IsLoggedIn);
}
This unit test has no errors, but the test fails (I think it is because I am passing it any HttpResponseMessage? And I need to somehow pass it HttpStatusCode.Accepted? How would I do that?
Take note of the usage of the method, how it passes HttpStatusCode.Unauthorized, then can I do something like that with HttpStatusCode.Accepted?:
var client = service.CreateHttpClientForAccessToken(x => x.StatusCode == HttpStatusCode.Unauthorized, CoreConstants.TimeoutMyDetails);
EDIT: To be clear, It is this line of code that I need to correct:
authenticatedRequestService.Setup(foo => foo.CreateHttpClientForAccessToken((It.IsAny<Func <HttpResponseMessage, bool>>())
, 0
)).Returns(new HttpClient());
EDIT: Whilst debugging the problem starts here (check the code comment after the client is created):
async Task<ServiceResponse> UpdateUserDetails()
{
// Have to late-resolve this otherwise we end up with a dependency loop
var service = Mvx.Resolve<IAuthenticatedRequestService>();
try
{
var client = service.CreateHttpClientForAccessToken(x => x.StatusCode == HttpStatusCode.Unauthorized, CoreConstants.TimeoutMyDetails);
// here is the problem, the client is null after this line of code.
var user = _user;
I have since found that it is not a Moq issue. MvvmCross is not registering the object to resolve correctly.
This line is not working:
Mvx.RegisterSingleton<IAuthenticatedRequestService>(authenticatedRequestService.Object);
as this line creates an AuthenticatedRequestService but it is not the mock one that I made:
var service = Mvx.Resolve<IAuthenticatedRequestService>();
Here is some context of resolving the AuthenticatedRequestService
async Task<ServiceResponse> UpdateUserDetails()
{
// Have to late-resolve this otherwise we end up with a dependency loop
var service = Mvx.Resolve<IAuthenticatedRequestService>();
try
{
var client = service.CreateHttpClientForAccessToken(x => x.StatusCode == HttpStatusCode.Unauthorized, CoreConstants.TimeoutMyDetails);
var user = _user;
var str = await client.GetStringAsync(new Uri(user.IdUrl));
var newUser = JsonConvert.DeserializeObject<User.Json>(str);
var token = _token;
if (token != null)