Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c# - Customizing AutoFixture builder with seeded property

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...