Unit testing with EF4 “Code First” and Repository

后端 未结 3 553
执笔经年
执笔经年 2020-12-04 12:13

I am attempting to get a handle on Unit testing a very simple ASP.NET MVC test app I\'ve built using the Code First approach in the latest EF4 CTP. I\'m not very experience

3条回答
  •  日久生厌
    2020-12-04 12:35

    You probably getting an error

     AutoFixture was unable to create an instance from System.Data.Entity.DbSet`1[...], most likely because it has no public constructor, is an abstract or non-public type.
    

    The DbSet has a protected constructor.

    The approach usually taken to support testability create your own implementation of the Db Set

    public class MyDbSet : IDbSet where T : class
    {
    

    Use this as

    public class MyContext : DbContext
    {
        public virtual MyDbSet Prices { get; set; }
    }
    

    If you look at the below SO question, 2ns answer, you should be able to find the complete implementation of a custom DbSet.

    Unit testing with EF4 "Code First" and Repository

    Then the

     var priceService = fixture.Create();
    

    Should not throw an exception.

提交回复
热议问题