定制AutoFixture建设者接种财产 [英] Customizing AutoFixture builder with seeded property

查看:141
本文介绍了定制AutoFixture建设者接种财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集成测试定制autofixture建设者。 code如下。

问题1 - 在present第一个事务有一个1 TransactionViewKey.TransactionId,等我如何设置TransactionViewKey TRANSACTIONID所以它是从方法参数播种的 beginningTransactionId 的?如返回TransactionViews,其中第一TRANSACTIONID为200,则每递增1的阵列?

问题2 - 拉姆达用于确定transactiondate似乎只运行一次 - 所以每个日期是相同的值。如何设置所以它运行每个生成的实例随机生成日期,而不是​​仅一次的建设者?

感谢

 静态TransactionView [] CreateTransactions(INT transactionsToReturnCount,长beginningTransactionId){
      随机随机=新的随机();
      IFixture夹具=新灯();
      fixture.Customize< TransactionViewKey>(OB => OB
                                    。随着(T => t.TransactionId)
                                    。随着(T => t.TransactionIdSpecified,真)
                                    .OmitAutoProperties()
                                    );
      fixture.Customize< TransactionView>(OB => OB
                                             。随着(T => t.TransactionDate,DateTime.Now - 新的时间跨度(random.Next(30),0,0,0))
                                             。随着(T => t.PostDate,DateTime.Now - 新的时间跨度(random.Next(30),0,0,0))
                                             。随着(T => t.ViewKey)
                                             。随着(T => t.Amount)
                                             .OmitAutoProperties()
          );
      IEnumerable的< TransactionView> transactionViews = fixture.CreateMany< TransactionView>(transactionsToReturnCount);
      返回transactionViews.OrderBy(T => t.TransactionDate).ToArray();
  }


解决方案

在我潜入回答具体问题,我想指出的东西,可能会轻松很多:你的可能的考虑只赋值给那些可写的属性你叫CreateMany后,但你之前返回结果。

事情是这样的:

  VAR transactionViews = fixture.CreateMany< TransactionView>(transactionsToReturnCount);
的foreach(在transactionViews VAR电视)
{
    tv.ViewKey.TransactionId = beginningTransactionId ++;
    tv.TransactionDate = DateTime.Now - 新的时间跨度(random.Next(30),0,0,0);
}
返回transactionViews.OrderBy(T => t.TransactionDate).ToArray();

这可能看起来像一个黑客,但实际上不是。 AutoFixture旨在创建匿名值所以每当你尝试分配特定的值(你目前是),你正在加紧其初衷之外。

不要误会我的意思:它很酷,你使用AutoFixture这样。我有时也有一些特定的值赋给我有AutoFixture创建标本的一些成员,但我倾向于使用上述技术,因为getter和setter方法​​的正常.NET API为已经专门在完全相同,这样做。 AutoFixture,另一方面,是专门在限定为是模糊规则,因此它的目的是相反的。

这是说,但是,我现在回答上述提出的具体问题:

问题1

我没试过编译,所以你可能需要调整了一点,但你可以做的最好的事情会是这样的:

  fixture.Customize< TransactionViewKey>(OB => OB
    。没有(T => t.TransactionId)
    。做(T => t.TransactionId = beginningTransactionId ++)
    。随着(T => t.TransactionIdSpecified,真)
    .OmitAutoProperties());

问题2

到随着方法的第二个参数是的的委托 - 这是一个的的,所以它仅被评估一次。

要每次评价它,你可以用同样的伎俩如上:

  fixture.Customize< TransactionView>(OB => OB
    。没有(T => t.TransactionDate)
    。做(T => t.TransactionDate = DateTime.Now - 新的时间跨度(random.Next(30),0,0,0))
    。随着(T => t.PostDate,DateTime.Now - 新的时间跨度(random.Next(30),0,0,0))
    。随着(T => t.ViewKey)
    。随着(T => t.Amount)
    .OmitAutoProperties());

请让我知道如果您有任何其他问题。

心连心

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

这篇关于定制AutoFixture建设者接种财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆