如何使用AutoFixture添加假的EF4 Rerefence(如果可能的话,还是没有)(How to add a fake EF4 Rerefence with AutoFixture ( or

编程入门 行业动态 更新时间:2024-10-25 07:26:00
如何使用AutoFixture添加假的EF4 Rerefence(如果可能的话,还是没有)(How to add a fake EF4 Rerefence with AutoFixture ( or without if possible ))

我有以下代码:

public class FakeOrderRepository:IOrderRepository { private static Fixture fixture = new Fixture(); private List<acc_ORDERS> dbmock = new List<acc_ORDERS>() { fixture.Build<acc_ORDERS>().With(x => x.OrderNumber,Tests.FAKE_ORDERNUMBER) .Without(x => x.EntityKey) .Without(x => x.usr_CUSTOMERSReference) .Without(x => x.usr_CUSTOMERS) .Without(x => x.sys_COUNTRY1Reference) .Without(x => x.sys_COUNTRYReference) .Without(x => x.sys_STATE1Reference) .Without(x => x.sys_STATEReference) .Without(x => x.acc_CUSTOMJEWEL_ORDER_ITEMS) .Without(x => x.acc_DIAMOND_ORDER_ITEMS) .Without(x => x.acc_JEWELRY_ORDER_ITEMS) .Without(x => x.acc_CASHFLOW) .Without(x => x.sys_STATE) .Without(x => x.sys_STATE1) .Without(x => x.sys_COUNTRY) .Without(x => x.sys_COUNTRY1) .CreateAnonymous() }; public int Save(Order orderdto) { throw new NotImplementedException(); } public Order GetOrderByOrderNumber(int orderNumber) { try { var orderdto = dbmock.Where(x => x.OrderNumber == orderNumber).SingleOrDefault(); orderdto.sys_COUNTRYReference= new EntityReference<sys_COUNTRY>() { Value = new sys_COUNTRY() }; var order = Mapper.Map<acc_ORDERS, Order>(orderdto); return order; } catch (Exception ex) { throw new Exception("when asked to get the order with the ordernumber:" + orderNumber + " an error occured\r\n" + ex.Message); } } }

好像这一行:

orderdto.sys_COUNTRYReference= new EntityReference<sys_COUNTRY>() { Value = new sys_COUNTRY() };

当执行导致异常时,我试图模拟引用对象,如果我调用没有它的代码就像它一切正常,但我的测试代码需要sys_COUNTRYReference对象。

例外情况:

当此RelatedEnd的所有者为null时,不允许请求的操作。 使用默认构造函数创建的RelatedEnd对象应仅在序列化期间用作容器。

请提供有关如何解决此问题或从不同角度攻击它的建议。

谢谢。

I have the following code:

public class FakeOrderRepository:IOrderRepository { private static Fixture fixture = new Fixture(); private List<acc_ORDERS> dbmock = new List<acc_ORDERS>() { fixture.Build<acc_ORDERS>().With(x => x.OrderNumber,Tests.FAKE_ORDERNUMBER) .Without(x => x.EntityKey) .Without(x => x.usr_CUSTOMERSReference) .Without(x => x.usr_CUSTOMERS) .Without(x => x.sys_COUNTRY1Reference) .Without(x => x.sys_COUNTRYReference) .Without(x => x.sys_STATE1Reference) .Without(x => x.sys_STATEReference) .Without(x => x.acc_CUSTOMJEWEL_ORDER_ITEMS) .Without(x => x.acc_DIAMOND_ORDER_ITEMS) .Without(x => x.acc_JEWELRY_ORDER_ITEMS) .Without(x => x.acc_CASHFLOW) .Without(x => x.sys_STATE) .Without(x => x.sys_STATE1) .Without(x => x.sys_COUNTRY) .Without(x => x.sys_COUNTRY1) .CreateAnonymous() }; public int Save(Order orderdto) { throw new NotImplementedException(); } public Order GetOrderByOrderNumber(int orderNumber) { try { var orderdto = dbmock.Where(x => x.OrderNumber == orderNumber).SingleOrDefault(); orderdto.sys_COUNTRYReference= new EntityReference<sys_COUNTRY>() { Value = new sys_COUNTRY() }; var order = Mapper.Map<acc_ORDERS, Order>(orderdto); return order; } catch (Exception ex) { throw new Exception("when asked to get the order with the ordernumber:" + orderNumber + " an error occured\r\n" + ex.Message); } } }

Seems like this line:

orderdto.sys_COUNTRYReference= new EntityReference<sys_COUNTRY>() { Value = new sys_COUNTRY() };

When executed causes an exception, I am trying to mock the reference objects, if I call the code without this like It all works, but my test code needs the sys_COUNTRYReference object.

The Exception:

Requested operation is not allowed when the owner of this RelatedEnd is null. RelatedEnd objects that were created with the default constructor should only be used as a container during serialization.

Please advice on how to fix this problem or attack it from a different angle.

Thank you.

最满意答案

根据我的经验,AutoFixture与EF EntityObject实体不兼容。 原因是它想要分配对象的每个属性。 是的,您可以告诉它使用.Without()跳过某些属性,但是当您只想“伪造”大约一半的属性时,这变得非常繁琐。 您的示例代码仅涉及表面,并且已经模糊了测试的目的。

我无法找到一种方法来阻止AutoFixture分配给某些类型,特别是开放的泛型类型。

所以我认为你不能使用AutoFixture(但它! - 它是开源的,有人可以提供修复......)。

另一方面,我将不同意@Ladislav - 使用EntityObject实体进行测试工作正常。 AutoFixture似乎对此无动于衷,但这并不意味着你无法测试! 您只需要直接为AutoFixture以外的其他内容分配测试值。

你当然可以做一些事情:

myEntity = new SomeEntity { Foo = fixture.CreateAnonymous<string>(), Bar = fixture.CreateAnonymous<int>() };

..等等。

AutoFixture doesn't work well with EF EntityObject entities, in my experience. The reason is that it wants to assign every property of the object. Yes, you can tell it to skip certain properties with .Without(), but this becomes quite tedious when you only want to "fake" around half of the properties you have. Your example code only scratches the surface and is already obscuring the purpose of the test.

I was unable to find a way to stop AutoFixture from ever assigning to certain types, especially open generic types.

So I don't think you'll be able to use AutoFixture for this (yet! -- it's open source, and someone could contribute a fix...).

On the other hand, I'm going to disagree with @Ladislav -- testing with EntityObject entities works fine. AutoFixture seems pretty indifferent to this, but that doesn't mean you can't test! You just have to assign the test values with something other than AutoFixture directly.

You can certainly do something along the lines of:

myEntity = new SomeEntity { Foo = fixture.CreateAnonymous<string>(), Bar = fixture.CreateAnonymous<int>() };

..etc.

更多推荐

本文发布于:2023-08-04 03:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   AutoFixture   fake   add   Rerefence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!