Reputation: 3799
public partial class TestObjectCode
{
/// <summary>
/// We don't make constructor public and forcing to create object using
/// <see cref="Create"/> method.
/// But constructor can not be private since it's used by EntityFramework.
/// Thats why we did it protected.
/// </summary>
protected TestObjectCode() {}
public static TestObjectCode Create(
DateTime executiontime,
Int32 conditionid,
String conditionname)
{
var @objectToReturn = new TestObjectCode
{
ExecutionTime = executiontime,
ConditionId = conditionid,
ConditionName = conditionname
};
return @objectToReturn;
}
public virtual Int32 ConditionId { get; set; }
public virtual String ConditionName { get; set; }
public virtual DateTime ExecutionTime { get; set; }
}
Test:
[Test]
[TestCase("1/1/2015", "07/5/2016")]
public void Task Should_Filter_By_Date_Range_Only(string startDate, string endDate)
{
//Arrange
var startDateTime = DateTime.Parse(startDate);
var endDateTime = DateTime.Parse(endDate);
//get randomDate between two Dates
TimeSpan timeSpan = endDateTime - startDateTime;
var randomTest = new Random();
TimeSpan newSpan = new TimeSpan(0, randomTest.Next(0, (int)timeSpan.TotalMinutes), 0);
DateTime newDate = startDateTime + newSpan;
var list = new List<TestObjectCode>();
_fixture.AddManyTo(list);
_fixture.Customize<TestObjectCode>(
c => c
.With(x => x.ExecutionTime, newDate)
.With(x => x.ConditionId, 1)
);
_fixture.RepeatCount = 7;
_fixture.AddManyTo(list);
}
Above test fails due to _fixture.Customize and my ctor being proetced. if i make it public it works, but i would like to make it proected. this class has 15 more properties i did not list here. also i wanted a randmon date between two dateRanges for each item in list.
How can i call the factory Create method ? do i need to do define autoFixure for each property?
Ploeh.AutoFixture.ObjectCreationException : The decorated ISpecimenBuilder could not create a specimen based on the request: EMR.Entities.AbpAuditLogs. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build expression, try supplying a factory using one of the IFactoryComposer methods.
Upvotes: 2
Views: 2164
Reputation: 233150
You can make the above test pass by adding .FromFactory(new MethodInvoker(new FactoryMethodQuery()))
to your customization:
fixture.Customize<TestObjectCode>(
c => c
.FromFactory(new MethodInvoker(new FactoryMethodQuery()))
.With(x => x.ExecutionTime, newDate)
.With(x => x.ConditionId, 1));
Both of these classes are defined in the Ploeh.AutoFixture.Kernel
namespace.
All that said, though, you should strongly reconsider your overall approach.
Upvotes: 3