mirror of
https://github.com/bitwarden/server.git
synced 2025-07-13 05:38:25 -05:00
Enable testing of ASP.net MVC controllers
Controller properties have all kinds of validations in the background. In general, we don't user properties on our Controllers, so the easiest way to allow for Autofixture-based testing of our Controllers is to just omit setting all properties on them.
This commit is contained in:
41
test/Common/AutoFixture/BuilderWithoutAutoProperties.cs
Normal file
41
test/Common/AutoFixture/BuilderWithoutAutoProperties.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Dsl;
|
||||
using AutoFixture.Kernel;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture
|
||||
{
|
||||
public class BuilderWithoutAutoProperties : ISpecimenBuilder
|
||||
{
|
||||
private readonly Type _type;
|
||||
public BuilderWithoutAutoProperties(Type type)
|
||||
{
|
||||
_type = type;
|
||||
}
|
||||
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var type = request as Type;
|
||||
if (type == null || type != _type)
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
|
||||
var fixture = new Fixture();
|
||||
// This is the equivalent of _fixture.Build<_type>().OmitAutoProperties().Create(request, context), but no overload for
|
||||
// Build(Type type) exists.
|
||||
dynamic reflectedComposer = typeof(Fixture).GetMethod("Build").MakeGenericMethod(_type).Invoke(fixture, null);
|
||||
return reflectedComposer.OmitAutoProperties().Create(request, context);
|
||||
}
|
||||
}
|
||||
public class BuilderWithoutAutoProperties<T> : ISpecimenBuilder
|
||||
{
|
||||
public object Create(object request, ISpecimenContext context) =>
|
||||
new BuilderWithoutAutoProperties(typeof(T)).Create(request, context);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user