I've got a customized autofixture builder for an integration test. Code is below.
Question 1 - At present the first transaction has a TransactionViewKey.TransactionId of 1, etc. How do I set the TransactionViewKey TransactionId so it is seeded from the method param beginningTransactionId? eg returning an array of TransactionViews where the first TransactionId is 200, then each incrementing by 1?
Question 2 - the lambda for determining transactiondate seems to be run once only - and so each date is the same value. How do I setup the builder so it runs the random date generator for each generated instance rather than once only?
thanks
static TransactionView[] CreateTransactions(int transactionsToReturnCount, long beginningTransactionId) {
Random random = new Random();
IFixture fixture = new Fixture();
fixture.Customize<TransactionViewKey>(ob => ob
.With(t => t.TransactionId)
.With(t => t.TransactionIdSpecified, true)
.OmitAutoProperties()
);
fixture.Customize<TransactionView>(ob => ob
.With(t => t.TransactionDate, DateTime.Now - new TimeSpan(random.Next(30),0,0,0))
.With(t => t.PostDate, DateTime.Now - new TimeSpan(random.Next(30), 0, 0, 0))
.With(t => t.ViewKey)
.With(t => t.Amount)
.OmitAutoProperties()
);
IEnumerable<TransactionView> transactionViews = fixture.CreateMany<TransactionView>(transactionsToReturnCount);
return transactionViews.OrderBy(t => t.TransactionDate).ToArray();
}
See Question&Answers more detail:os