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();
}
Before I dive into answering the specific questions, I'd like to point out something that might be a lot easier: you might consider just assigning values to those writable properties after you called CreateMany, but before you return the result.
Something like this:
var transactionViews = fixture.CreateMany<TransactionView>(transactionsToReturnCount);
foreach (var tv in transactionViews)
{
tv.ViewKey.TransactionId = beginningTransactionId++;
tv.TransactionDate = DateTime.Now - new TimeSpan(random.Next(30),0,0,0);
}
return transactionViews.OrderBy(t => t.TransactionDate).ToArray();
This might look like a hack, but really isn't. AutoFixture is designed to create Anonymous Values so whenever you are trying to assign specific values (which you currently are), you are stepping outside of its original purpose.
Don't get me wrong: it's cool that you use AutoFixture like this. I also sometimes have to assign some specific values to some members of the specimens I have AutoFixture create, but I tend to use the above technique, because the normal .NET API of getters and setter is already specialized at doing exactly that. AutoFixture, on the other hand, is specialized at defining rules for being vague, so its purpose is the opposite.
That said, however, I will now answer the specific questions put forth above:
Question 1
I haven't tried to compile this, so you may have to tweak it a bit, but the best thing you can do would be something like this:
fixture.Customize<TransactionViewKey>(ob => ob
.Without(t => t.TransactionId)
.Do(t => t.TransactionId = beginningTransactionId++)
.With(t => t.TransactionIdSpecified, true)
.OmitAutoProperties());
Question 2
The second parameter to the With method is not a delegate - it's a value so it's only being evaluated once.
To evaluate it each time, you can use the same trick as above:
fixture.Customize<TransactionView>(ob => ob
.Without(t => t.TransactionDate)
.Do(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());
Please let me know if you have any additional questions.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With