开发者

How to add a fake EF4 Rerefence with AutoFixture ( or without if possible )

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>()
                             开发者_Python百科                  {
                                                   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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜