Using autofixture in my data integration tests to create proxies

后端 未结 1 377
庸人自扰
庸人自扰 2021-01-18 23:50

I\'m trying to write a suite of database integration tests for my domain which uses Entity Framework. I would prefer to autofixture objects in some scenarios. My ideal synt

1条回答
  •  失恋的感觉
    2021-01-19 00:09

    Would something like this work?

    var fixture = new Fixture();
    fixture.Customize(c => c
        .FromFactory(ctx => ctx.Set.Create()));
    
    using (var context = new MyDbContext())
    {
        fixture.Inject(context);
        var item = fixture.CreateAnonymous();
        context.Set().Add(item);
        context.SaveChanges();
    }
    

    Disclaimer: I haven't tried to compile this...


    FWIW, if you were using xUnit.net with AutoFixture, you could reduce the test to something like:

    [Theory, MyAutoData]
    public void TheTest([Frozen]MyDbContext context, MyEntity item)
    {
        context.Set().Add(item);
        context.SaveChanges();
    }
    

    0 讨论(0)
提交回复
热议问题