How to Automoq concrete class dependencies through Automoq? - unit-testing

In my class constrctur, we had multiple concrete class dependencies. as per automoq documentation, we can only interface or abstraction.
System Under Test Class, in that ManageLocationRepository is concrete class dependency.
public class CityEventListener : IEvent<LocationChangeEventData>
{
private readonly ILocationAdapterCaller _locationAdapterCaller;
private readonly ManageLocationRepository _managerLocationRepository;
public CityEventListener(ILocationAdapterCaller locationAdapterCaller, ManageLocationRepository managerLocationRepository)
{
_locationAdapterCaller = locationAdapterCaller;
_managerLocationRepository = managerLocationRepository;
}
public async Task<bool> ProcessEvent(LocationChangeEventData eventData)
{
}
}
Test Case -
[Theory(DisplayName = "Valid value test")]
[ClassAutoMoqData(typeof(ValidValueTests))]
public async Task ProcessEvent_WithCreateOrUpdateOperation_CallsUpsertCityAndReturnsResult(ExpectedValueTestData<Parameters, bool> data,
[Frozen] Mock<ILocationAdapterCaller> locationAdapterCallerMock, [Frozen] Mock<ManageLocationRepository> managerLocationRepositoryMock,
CityEventListener sut)
{
// fakes
var cityDetail = _fixture.Build<CityDetail>()
.With(x => x.Id, data.Params.locationChangeEventData.Id).Create();
// Arrange
locationAdapterCallerMock.Setup(mock => mock.GetCityDetail(data.Params.locationChangeEventData.Id))
.ReturnsAsync(cityDetail).Verifiable();
managerLocationRepositoryMock
.Setup(mock => mock.UpsertCity(cityDetail))
.ReturnsAsync(data.ExpectedValue).Verifiable();
var result = await sut.ProcessEvent(data.Params.locationChangeEventData);
// Assert
using (new AssertionScope())
{
Assert.IsType<bool>(result);
Assert.Equal(data.ExpectedValue, result);
locationAdapterCallerMock.Verify();
managerLocationRepositoryMock.Verify();
}
}
ClassAutoMoq Attribute
public class ClassAutoMoqDataAttribute : CompositeDataAttribute
{
public ClassAutoMoqDataAttribute(Type values)
: base(new ClassDataAttribute(values), new AutoMoqDataAttribute())
{
}
}
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(() =>
{
var fixture = new Fixture().Customize(new CompositeCustomization(
new AutoMoqCustomization() { ConfigureMembers = true, GenerateDelegates = true },
new SupportMutableValueTypesCustomization()));
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
return fixture;
})
{
}
}
what are the alternate way to moq such dependencies through automoq attribute.

I solved my problem by taking two actions -
mark concrete class method virtual
create custom class automoq attribute to freeze dependencies
public class CityEventListenerClassAutoMoqAttribute : CompositeDataAttribute
{
public CityEventListenerClassAutoMoqAttribute(Type values)
: base(new ClassDataAttribute(values), new CityEventListenerAutoMoqAttribute())
{
}
}
public class CityEventListenerAutoMoqAttribute : AutoDataAttribute
{
public CityEventListenerAutoMoqAttribute()
: base(() =>
{
var fixture = new Fixture().Customize(new CompositeCustomization(
new AutoMoqCustomization() { ConfigureMembers = true, GenerateDelegates = true },
new SupportMutableValueTypesCustomization()));
var managerLocationRepositoryMock =
fixture.Freeze<Mock<ManageLocationRepository>>();
fixture.Inject(managerLocationRepositoryMock.Object);
return fixture;
})
{
}
}
Now my test case looks like this -
[Theory(DisplayName = "Valid value test")]
[CityEventListenerClassAutoMoq(typeof(ValidValueTests))]
public async Task ProcessEvent_WithCreateOrUpdateOperation_CallsUpsertCityAndReturnsResult(ExpectedValueTestData<Parameters, bool> data,
[Frozen] Mock<ILocationAdapterCaller> locationAdapterCallerMock, [Frozen] Mock<ManageLocationRepository> managerLocationRepositoryMock,
CityEventListener sut)
{
// fakes
var cityDetail = _fixture.Build<CityDetail>()
.With(x => x.Id, data.Params.locationChangeEventData.Id).Create();
// Arrange
locationAdapterCallerMock.Setup(mock => mock.GetCityDetail(data.Params.locationChangeEventData.Id))
.ReturnsAsync(cityDetail).Verifiable();
managerLocationRepositoryMock
.Setup(mock => mock.UpsertCity(cityDetail))
.ReturnsAsync(data.ExpectedValue).Verifiable();
var result = await sut.ProcessEvent(data.Params.locationChangeEventData);
// Assert
using (new AssertionScope())
{
Assert.IsType<bool>(result);
Assert.Equal(data.ExpectedValue, result);
locationAdapterCallerMock.Verify();
managerLocationRepositoryMock.Verify();
}
}
Do let me know if I can improve anything in my approach.

Related

Mock DBSet<T> and call AsQueryable method return an empty list

I use entity framework core 6, XUnit for testing, Moq 4.16.1
I have a List, Mock it as DBSet<T>, I call AsQueryable method on DBSet<T> then return a empty list. If i mock AsQueryable method on DBSet<T>, i will have a expected list. Can you tell me Why?
This is my code
public static class MockDbExtensions
{
public static Mock<DbSet<T>> MockDbSet<T>(this IQueryable<T> testData) where T : class
{
Mock<DbSet<T>> dbSetMock = new Mock<DbSet<T>>();
dbSetMock.As<IQueryable<T>>().Setup(m => m.Provider).Returns(new TestAsyncQueryProvider<T>(testData.Provider));
dbSetMock.As<IQueryable<T>>().Setup(m => m.Expression).Returns(testData.Expression);
dbSetMock.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(testData.ElementType);
dbSetMock.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => testData.GetEnumerator());
dbSetMock.As<IAsyncEnumerable<T>>().Setup(m => m.GetAsyncEnumerator(default)).Returns(() => new TestAsyncEnumerator<T>(testData.GetEnumerator()));
return dbSetMock;
}
public static Mock<DbSet<T>> MockDbSet<T>(this IEnumerable<T> elements) where T : class
{
var testData = elements.AsQueryable();
return testData.MockDbSet();
}
public static Mock<DbSet<T>> MockDbSet<T>(this T[] elements) where T : class
{
var testData = elements.AsQueryable();
return testData.MockDbSet();
}
}
internal class TestAsyncQueryProvider<TEntity> : IAsyncQueryProvider
{
private readonly IQueryProvider _inner;
internal TestAsyncQueryProvider(IQueryProvider inner)
{
_inner = inner;
}
public IQueryable CreateQuery(Expression expression)
{
return new TestAsyncEnumerable<TEntity>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
return new TestAsyncEnumerable<TElement>(expression);
}
public object Execute(Expression expression)
{
return _inner.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return _inner.Execute<TResult>(expression);
}
public IAsyncEnumerable<TResult> ExecuteAsync<TResult>(Expression expression)
{
return new TestAsyncEnumerable<TResult>(expression);
}
public TResult ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = new CancellationToken())
{
var expectedResultType = typeof(TResult).GetGenericArguments()[0];
var executionResult = ((IQueryProvider)this).Execute(expression);
return (TResult)typeof(Task).GetMethod(nameof(Task.FromResult))
.MakeGenericMethod(expectedResultType)
.Invoke(null, new[] { executionResult });
}
}
internal class TestAsyncEnumerable<T> : EnumerableQuery<T>, IAsyncEnumerable<T>, IQueryable<T>
{
public TestAsyncEnumerable(IEnumerable<T> enumerable)
: base(enumerable)
{ }
public TestAsyncEnumerable(Expression expression)
: base(expression)
{ }
public IAsyncEnumerator<T> GetEnumerator()
{
return new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
IQueryProvider IQueryable.Provider => new TestAsyncQueryProvider<T>(this);
}
internal class TestAsyncEnumerator<T> : IAsyncEnumerator<T>
{
private readonly IEnumerator<T> _inner;
public TestAsyncEnumerator(IEnumerator<T> inner)
{
_inner = inner;
}
public void Dispose()
{
_inner.Dispose();
}
public T Current => _inner.Current;
public Task<bool> MoveNext(CancellationToken cancellationToken)
{
return Task.FromResult(_inner.MoveNext());
}
public ValueTask<bool> MoveNextAsync() => new ValueTask<bool>(_inner.MoveNext());
public ValueTask DisposeAsync()
{
_inner.Dispose();
return new ValueTask();
}
}
public class ReportTemplateServiceTests
{
private List<ReportTemplateType> CreateReportTemplateTypes()
{
return new List<ReportTemplateType>
{
new ReportTemplateType { Id = new Guid("144013ED-2D77-4572-8AB4-483C66E4EF1D"), Name = "Financial statements", DisplayOrder = 3, CmsId = 101475, IsActive = true },
new ReportTemplateType { Id = new Guid("058F8F85-A735-48F3-B526-79855FFF1EB0"), Name = "Reports", DisplayOrder = 1, CmsId = 100473, IsActive = true },
new ReportTemplateType { Id = new Guid("69D55B31-69BB-4BBC-82F1-A707B484E99B"), Name = "Letters", DisplayOrder = 2, CmsId = 100474, IsActive = true },
new ReportTemplateType { Id = new Guid("19D2650B-CE8E-4B54-95D9-ACAFA5C1CE40"), Name = "Custom working papers", DisplayOrder = 4, CmsId = 101476, IsActive = true },
};
}
public async void CreateReportTemplateAsync_Success()
{
// Arrange
var reportTemplates = CreateReportTemplates().MockDbSet().Object;
var a = reportTemplates.AsQueryable().ToList(); // return empty list after call AsQueryable()
}
}
I mock AsQueryable method on DBSet<T>, i will have a expected list.

unit test for MediatR in .net6 with generic repo returns null repository

I am using MediatR for command query segregation.
I want to test the command method, my command method accept a clientappsetting model as an input .here you can see my whole handler and command code :
AddClientAppSettingCommandHandler : IRequestHandler<AddClientAppSettingCommand, AddClientAppSettingResponse>
{
private readonly ICurrentUserService _userService;
private readonly IRepository<ClientAppSettings> _repository;
public AddClientAppSettingCommandHandler(ICurrentUserService userService,
IRepositoryAccessor repositoryAccessor)
{
_userService = userService;
_repository = repositoryAccessor.GetRepository<ClientAppSettings>(_userService.CustomerIsin,
reThrowException: true, type: DatabaseType.Raven);
}
public async Task<AddClientAppSettingResponse> Handle(AddClientAppSettingCommand request,
CancellationToken cancellationToken)
{
var entity = new ClientAppSettings(_userService.CustomerIsin)
{
LightTheme = request.Setting.LightTheme,
Order = request.Setting.Order,
Notch = request.Setting.Notch,
PageSize = request.Setting.PageSize,
ApplyCommissionInPortfolio = request.Setting.ApplyCommissionInPortfolio,
UseClosingPriceInPortfolioTotalValue = request.Setting.UseClosingPriceInPortfolioTotalValue,
ShowNotifications = request.Setting.ShowNotifications,
NoSleep = request.Setting.NoSleep,
NoBalance = request.Setting.NoBalance,
DataTracker = request.Setting.DataTracker,
UserStatusBarToUp = request.Setting.UserStatusBarToUp,
PortfolioBasedOnLastPositivePeriod = request.Setting.PortfolioBasedOnLastPositivePeriod,
};
var cRepository = CacheableRepository<ClientAppSettings>.From(_repository);
var result = await cRepository.AddOrUpdateAsync(entity);
if (!result.IsSucceeded)
throw new EasyException(EasyException.DATABASE_EXCEPTION, result.Error);
return AddClientAppSettingResponse.Map(entity);
}
As you can see my handler has two dependencies ICurrentUserService , IRepositoryAccessor
My problem is IRepositoryAccessor when I run the test the repository object is null .
Here is my repository interface and imp ;
public interface IRepositoryAccessor
{
IRepository<TEntity> GetRepository<TEntity>(
string shard = "public",
DatabaseType type = DatabaseType.Raven,
Type inheritedRepository = null,
bool manualDisposing = false,
bool reThrowException=false) where TEntity : BaseEntity;
void CloseSession();
}
The imp :
public sealed class RepositoryAccessor : IRepositoryAccessor, IDisposable
{
private static readonly Dictionary<Type, object> FlyweightSqlGenerator = new();
private readonly List<IDisposable> _sessions = new();
private readonly ITracer _tracer;
private readonly IConfiguration _configuration;
public RepositoryAccessor(IConfiguration configuration, ITracer tracer = null)
{
_configuration = configuration;
_tracer = tracer;
}
public void CloseSession()
{
for (int i = 0; i < _sessions.Count; i++)
{
_sessions[i].Dispose();
}
_sessions.Clear();
}
public void Dispose() => CloseSession();
public IRepository<TEntity> GetRepository<TEntity>(
string shard = "public",
DatabaseType type = DatabaseType.Raven,
Type inheritedRepository = null,
bool manualDisposing = false,
bool reThrowException = false) where TEntity : BaseEntity
{
if (type == DatabaseType.Raven)
{
return GetRavenRepository<TEntity>(inheritedRepository, shard, manualDisposing, reThrowException);
}
else if (type == DatabaseType.Redis)
{
return new RedisRepository<TEntity>();
}
return GetSQLRepository<TEntity>(inheritedRepository, manualDisposing, reThrowException);
}
}
And here is my test :
[Fact]
public async void Test1()
{
//Arange
var mediator = new Mock<IMediator>();
var userservice = new Mock<ICurrentUserService>();
var repo = new Mock<IRepositoryAccessor>();
AddClientAppSettingCommand command = new AddClientAppSettingCommand(new domain.Entities.ClientAppSettings());
AddClientAppSettingCommandHandler handler = new AddClientAppSettingCommandHandler(userservice.Object,repo.Object);
//Act
var x = await handler.Handle(command, new System.Threading.CancellationToken());
}
And when I run the test with debug mode my repository is null :
I should define IAccessor and IRepository as a mock and setting up the Irepository for IAccessor as you can see :
var repoacc = new Mock<IRepositoryAccessor>();
var repo = new Mock<domain.Interfaces.IRepository<ClientAppSettings>>();
repoacc.Setup(i => i.GetRepository<ClientAppSettings>(It.IsAny<string>(), DatabaseType.Raven, null, false, true)).Returns(repo.Object);

ContosoUniversityDotNetCore-Pages SliceFixture with AutoMapper

My Create handler uses AutoMapper to map from command to entity.
public async Task<int> Handle(Command command, CancellationToken cancellationToken)
{
var entity = _mapper.Map<ExampleEntity>(command);
await _db.ExampleEntity.AddAsync(entity, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entity.Id;
}
My test looks like this
var command = new Create.Command
{
Name = "Example 1",
...
};
var id = await _fixture.SendAsync(command, mapper);
id.ShouldNotBeNull();
This is the SliceFixture
[CollectionDefinition(nameof(SliceFixture))]
public class SliceFixtureCollection : ICollectionFixture<SliceFixture> { }
public class SliceFixture : IAsyncLifetime
{
private readonly Checkpoint _checkpoint;
private readonly IConfiguration _configuration;
private readonly IServiceScopeFactory _scopeFactory;
private readonly WebApplicationFactory<Startup> _factory;
public SliceFixture()
{
_factory = new ContosoTestApplicationFactory();
_configuration = _factory.Services.GetRequiredService<IConfiguration>();
_scopeFactory = _factory.Services.GetRequiredService<IServiceScopeFactory>();
_checkpoint = new Checkpoint();
}
public class ContosoTestApplicationFactory : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((_, configBuilder) =>
{
configBuilder.AddInMemoryCollection(new Dictionary<string, string>
{
{"ConnectionStrings:DefaultConnection", _connectionString}
});
});
}
private readonly string _connectionString = "";
}
public async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)
{
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<SchoolContext>();
try
{
await dbContext.BeginTransactionAsync();
await action(scope.ServiceProvider);
await dbContext.CommitTransactionAsync();
}
catch (Exception)
{
dbContext.RollbackTransaction();
throw;
}
}
public async Task<T> ExecuteScopeAsync<T>(Func<IServiceProvider, Task<T>> action)
{
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<SchoolContext>();
try
{
await dbContext.BeginTransactionAsync();
var result = await action(scope.ServiceProvider);
await dbContext.CommitTransactionAsync();
return result;
}
catch (Exception)
{
dbContext.RollbackTransaction();
throw;
}
}
public Task ExecuteDbContextAsync(Func<SchoolContext, Task> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>()));
public Task ExecuteDbContextAsync(Func<SchoolContext, ValueTask> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>()).AsTask());
public Task ExecuteDbContextAsync(Func<SchoolContext, IMediator, Task> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>(), sp.GetService<IMediator>()));
public Task<T> ExecuteDbContextAsync<T>(Func<SchoolContext, Task<T>> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>()));
public Task<T> ExecuteDbContextAsync<T>(Func<SchoolContext, ValueTask<T>> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>()).AsTask());
public Task<T> ExecuteDbContextAsync<T>(Func<SchoolContext, IMediator, Task<T>> action) => ExecuteScopeAsync(sp => action(sp.GetService<SchoolContext>(), sp.GetService<IMediator>()));
public Task InsertAsync<T>(params T[] entities) where T : class
{
return ExecuteDbContextAsync(db =>
{
foreach (var entity in entities)
{
db.Set<T>().Add(entity);
}
return db.SaveChangesAsync();
});
}
public Task<T> FindAsync<T>(int id)
where T : class, IEntity
{
return ExecuteDbContextAsync(db => db.Set<T>().FindAsync(id).AsTask());
}
public Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
return ExecuteScopeAsync(sp =>
{
var mediator = sp.GetRequiredService<IMediator>();
return mediator.Send(request);
});
}
public Task SendAsync(IRequest request)
{
return ExecuteScopeAsync(sp =>
{
var mediator = sp.GetRequiredService<IMediator>();
return mediator.Send(request);
});
}
private int _courseNumber = 1;
public int NextCourseNumber() => Interlocked.Increment(ref _courseNumber);
public Task InitializeAsync() => _checkpoint.Reset(_configuration.GetConnectionString("DefaultConnection"));
public Task DisposeAsync()
{
_factory?.Dispose();
return Task.CompletedTask;
}
}
When I run the test I get -
Object reference not set to an instance of an object.
because _mapper is null.
I know I probably need to modify the SliceFixture but can't figure out what to do.
I was looking to implement Jimmy’s example here with the additional AutoMapper implementation.

.net core xunit How verify if method was called from inherited class

I have a base class called BaseService, and i want to verify if method IsValid was called in my unit test.
public interface IBaseService
{
bool IsValid<Dto, DtoValidator>(Dto entityDto, DtoValidator validator) where DtoValidator : AbstractValidator<Dto>;
}
public class BaseService : IBaseService
{
protected readonly IMapper _mapper;
protected readonly INotificationService _notification;
public BaseService(
IMapper mapper,
INotificationService notification)
{
_mapper = mapper;
_notification = notification;
}
public virtual bool IsValid<Dto, DtoValidator>(Dto entityDto, DtoValidator validator) where DtoValidator : AbstractValidator<Dto>
{
var result = validator.Validate(entityDto);
foreach (var error in result.Errors)
{
_notification.Notify(error.ErrorMessage);
}
return result.IsValid;
}
}
Class that use BaseService , some parts of code was omited
public interface IAfiliadoService : IBaseService {}
public class AfiliadoService : BaseService, IAfiliadoService
{
public AfiliadoService(
IMapper mapper,
INotificationService notification) : base(mapper, notification)
{
_afiliadoRepository = afiliadoRepository;
_lojaRepository = lojaRepository;
}
public async Task<AfiliadoResponseDto> AddAsync(AddAfiliadoRequestDto request)
{
if (IsValid(request, new AddAfiliadoRequestDtoValidator()))
{
}
}
}
In my test project i created those classes
public class MockBaseService
{
public readonly Mock<IMapper> _mapper;
public readonly Mock<INotificationService> _notification;
public MockBaseService()
{
_mapper = new Mock<IMapper>();
_notification = new Mock<INotificationService>();
}
}
public class AfiliadoServiceTest : MockBaseService
{
private readonly IAfiliadoService _afiliadoService;
public AfiliadoServiceTest()
{
_afiliadoService = new AfiliadoService(
_mapper.Object,
_notification.Object);
}
[Fact]
public async Task AdicionarUsuarioComEmailQueJaExisteDeveNotificar()
{
var request = new AddAfiliadoRequestDto();
var result = await _afiliadoService.AddAsync(request);
Assert.Null(result);
_notification
.Verify(v => v.Notify(It.IsAny<string>()), Times.Once);
// Here i want verify if method IsValid was called Once, like the verification above
}
}
I tried some things but with no success.
Exemple with what i want.
var mock = new Mock<AfiliadoService>();
mock.Verify(v => v.IsValid(request, new AddAfiliadoRequestDtoValidator()), Times.Once);
var mock = new Mock<AfiliadoService>(_mapper.Object,
_notification.Object);
var result = await mock.Object.AddAsync(request);
mock.Verify(v => v.IsValid(request, new AddAfiliadoRequestDtoValidator()), Times.Once);
This last one i got the message
Message:
Moq.MockException :
Expected invocation on the mock once, but was 0 times: v => v.IsValid<AddAfiliadoRequestDto, AddAfiliadoRequestDtoValidator>(AddAfiliadoRequestDto, AddAfiliadoRequestDtoValidator)
Performed invocations:
Mock<AfiliadoService:1> (v):
BaseService.IsValid<AddAfiliadoRequestDto, AddAfiliadoRequestDtoValidator>(AddAfiliadoRequestDto, AddAfiliadoRequestDtoValidator)
What im doing wrong?
Im using .net core - 3.1
xunit - 2.4.1
Moq - 4.16.0

Unit testing generic htmlHelper methods with nunit

I'm new to nUnit and I've been tasked with creating unit tests for some htmlhelper extension methods.
How should I go about creating a unit test for the following method?
public static MvcHtmlString EnumDropDownListForOrderBy<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, bool orderById, string firstElement = null, object htmlAttributes = null)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
Type enumType = GetNonNullableModelType(metadata);
IEnumerable<TEnum> values = Enum.GetValues(enumType).Cast<TEnum>();
IEnumerable<SelectListItem> items = values.Select(value => new SelectListItem()
{
Text = value.GetAttributeFrom<DescriptionAttribute>(value.ToString()).Description,
Value = value.ToString(),
Selected = value.Equals(metadata.Model)
});
IEnumerable<SelectListItem> itemsFiltered = items.Where(e => !string.IsNullOrEmpty(e.Text)).AsEnumerable();
itemsFiltered = itemsFiltered.OrderBy(e => (orderById ? e.Text : e.Value));
return htmlHelper.DropDownListFor(
expression,
itemsFiltered,
firstElement,
htmlAttributes
);
}
Any help would be appreciated
Below is how you write a Unit Test for this. Note that since you have not specified that you use a Mock object framework I'm going to the poor man technique, which is the hand written stubs and mocks. There is also another helper method if you are using Moq.
It is important to note that, in order to simplify the code execution I have made couple of changes to your extension method, so the test would not fail unexpectedly. Checking for any unexpected behaver is a good defensive programming practice anyway.
Back to the tests.
SUT (System Under Test)
This is how the SUT (System Under Test) looks like and supporting types looks like. (Please feel free to modify to your need accordingly)
public static class MyHtmlHelper
{
public static MvcHtmlString EnumDropDownListForOrderBy<TModel, TEnum>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TEnum>> expression,
bool orderById, string firstElement = null, object htmlAttributes = null,
Func<ModelMetadata> fromLambFunc = null)
{
ModelMetadata metadata =
ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
Type enumType = GetNonNullableModelType(metadata);
IEnumerable<TEnum> values = Enum.GetValues(enumType).Cast<TEnum>();
IEnumerable<SelectListItem> items =
values.Select(value => new SelectListItem()
{
Text = GetText(value),
Value = value.ToString(),
Selected = value.Equals(metadata.Model)
});
IEnumerable<SelectListItem> itemsFiltered =
items.Where(e => !string.IsNullOrEmpty(e.Text)).AsEnumerable();
itemsFiltered = itemsFiltered.OrderBy(e => (orderById ? e.Text : e.Value));
return htmlHelper.DropDownListFor
(expression, itemsFiltered, firstElement, htmlAttributes);
}
private static Type GetNonNullableModelType(ModelMetadata metadata) {
return typeof (SomeEnum);
}
private static string GetText<TEnum>(TEnum value) {
return value.GetAttributeFrom<DescriptionAttribute>(value.ToString()) != null
? value.GetAttributeFrom<DescriptionAttribute>(value.ToString()).Description
: string.Empty;
}
}
public static class ExtensionMethodsAttr
{
public static T GetAttributeFrom<T>(this object instance, string propertyName)
where T : Attribute
{
var attrType = typeof(T);
var property = instance.GetType().GetProperty(propertyName);
return property != null ?
(T)property.GetCustomAttributes(attrType, false).First() : default(T) ;
}
}
public enum SomeEnum { A,}
Unit Tests
[TestFixture]
public class HtmlHelperTests
{
[Test]
public void EnumDropDownListForOrderBy_InvokeDropDownListFor_ReturnsExpectedSelectItemResult()
{
//Arrange
var expected = "<select id=\"Foo\" name=\"Foo\"></select>";
var fakeHtmlHelper = CreateHtmlHelperStaticStubs
(new ViewDataDictionary(new FakeViewModel() {Foo = SomeEnum.A}));
//var fakeHtmlHelper = CreateHtmlHelperUsingMoq
(new ViewDataDictionary(new FakeViewModel(){Foo = SomeEnum.A}));
//Act
var result = fakeHtmlHelper.EnumDropDownListForOrderBy
(model => model.Foo, It.IsAny<bool>(), null, null, null);
//Assert
Assert.AreEqual(expected, result.ToString());
}
private static HtmlHelper<FakeViewModel>
CreateHtmlHelperStaticStubs(ViewDataDictionary viewData)
{
var stubControllerContext = new ControllerContext(new FakeHttpContext(), new RouteData(), new FakeController());
var stubViewContext = new ViewContext(stubControllerContext, new FakeView(),
new ViewDataDictionary(new FakeViewModel() { Foo = SomeEnum.A }),
new TempDataDictionary(), new TextMessageWriter());
var fakeViewDataContainer = new FakeViewDataContainer();
fakeViewDataContainer.ViewData = viewData;
return new HtmlHelper<FakeViewModel>(stubViewContext, fakeViewDataContainer);
}
//Moq version
private static HtmlHelper<FakeViewModel>
CreateHtmlHelperUsingMoq(ViewDataDictionary viewData)
{
var stubControllerContext = new Mock<ControllerContext>();
stubControllerContext.Setup(x => x.HttpContext).Returns(new Mock<HttpContextBase>().Object);
stubControllerContext.Setup(x => x.RouteData).Returns(new RouteData());
stubControllerContext.Setup(x => x.Controller).Returns(new Mock<ControllerBase>().Object); ;
var stubViewContext = new Mock<ViewContext>();
stubViewContext.Setup(x => x.View).Returns(new Mock<IView>().Object);
stubViewContext.Setup(x => x.ViewData).Returns(viewData);
stubViewContext.Setup(x => x.TempData).Returns(new TempDataDictionary());
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(viewData);
return new HtmlHelper<FakeViewModel>(stubViewContext.Object, mockViewDataContainer.Object);
}
}
class FakeHttpContext : HttpContextBase
{
private Dictionary<object, object> _items = new Dictionary<object, object>();
public override IDictionary Items { get { return _items; } }
}
class FakeViewDataContainer : IViewDataContainer
{
private ViewDataDictionary _viewData = new ViewDataDictionary();
public ViewDataDictionary ViewData { get { return _viewData; } set { _viewData = value; } }
}
class FakeController : Controller { }
class FakeView : IView
{
public void Render(ViewContext viewContext, System.IO.TextWriter writer)
{
throw new NotImplementedException();
}
}
public class FakeViewModel {
public SomeEnum Foo { get; set; }
}