0

我们一直在试图弄清楚如何伪造 DbContext。所以我们按照这里的例子

http://romiller.com/2012/02/14/testing-with-a-fake-dbcontext/

和代码(一个用于 EF 上的抽象层(以下错误)和一个直接使用 EF(工作正常)...

我所能想到的是,这可能与预先生成的 EF 对象之上的抽象对象有关,没有表/实体之间关系的概念?任何想法?

我们一直在尝试找到一些方法来为我们的假货和真实代码建立一个共同的路径。也许简单的答案是停止这样做,只是模拟对存储库的调用,然后在那之后进行单元测试?

    CongressContext context = new CongressContext();
        var repo = new CongressRepository(context);

    //    IEnumerable<tMember> members = repo.Get100MembersDirectEF();
        IEnumerable<tMember> members = repo.Get100Members();

.....................

 public class CongressRepository : ICongressRepository
{
    ICongressContext db;
    CongressDb_DevEntities realDb = new CongressDb_DevEntities();

    public CongressRepository()
    {
        this.db = new CongressContext();
    }
    public CongressRepository(ICongressContext context)
    {
        this.db = context;
    }

    public List<tMember> Get100Members()
    {
        var members = db.Members.Where(x => x.MembersID < 100).ToList();
        return members;
    }

    public List<tMember> Get100MembersDirectEF()
    {
        realDb.Configuration.LazyLoadingEnabled = false;
        var members = realDb.tMembers.Where(x => x.MembersID < 100).ToList();
        return members;
    }





{"One or more validation errors were detected during model generation:\r\n\r\nCongress.Data.tEducationType: : EntityType 'tEducationType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tCongress: : EntityType 'tCongress' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tCongressMemPositionReason: : EntityType 'tCongressMemPositionReason' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tReasonType: : EntityType 'tReasonType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tDistrict: : EntityType 'tDistrict' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tParty: : EntityType 'tParty' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tPosition: : EntityType 'tPosition' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tChamber: : EntityType 'tChamber' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tState: : EntityType 'tState' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tMemMilitary: : EntityType 'tMemMilitary' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tMemOccupation: : EntityType 'tMemOccupation' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tOccupationType: : EntityType 'tOccupationType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tInterestGroup: : EntityType 'tInterestGroup' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tRaceType: : EntityType 'tRaceType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tReligion: : EntityType 'tReligion' has no key defined. Define the key for this EntityType.\r\ntEducationTypes: EntityType: EntitySet 'tEducationTypes' is based on type 'tEducationType' that has no keys defined.\r\ntCongresses: EntityType: EntitySet 'tCongresses' is based on type 'tCongress' that has no keys defined.\r\ntCongressMemPositionReasons: EntityType: EntitySet 'tCongressMemPositionReasons' is based on type 'tCongressMemPositionReason' that has no keys defined.\r\ntReasonTypes: EntityType: EntitySet 'tReasonTypes' is based on type 'tReasonType' that has no keys defined.\r\ntDistricts: EntityType: EntitySet 'tDistricts' is based on type 'tDistrict' that has no keys defined.\r\ntParties: EntityType: EntitySet 'tParties' is based on type 'tParty' that has no keys defined.\r\ntPositions: EntityType: EntitySet 'tPositions' is based on type 'tPosition' that has no keys defined.\r\ntChambers: EntityType: EntitySet 'tChambers' is based on type 'tChamber' that has no keys defined.\r\ntStates: EntityType: EntitySet 'tStates' is based on type 'tState' that has no keys defined.\r\ntMemMilitaries: EntityType: EntitySet 'tMemMilitaries' is based on type 'tMemMilitary' that has no keys defined.\r\ntMemOccupations: EntityType: EntitySet 'tMemOccupations' is based on type 'tMemOccupation' that has no keys defined.\r\ntOccupationTypes: EntityType: EntitySet 'tOccupationTypes' is based on type 'tOccupationType' that has no keys defined.\r\ntInterestGroups: EntityType: EntitySet 'tInterestGroups' is based on type 'tInterestGroup' that has no keys defined.\r\ntRaceTypes: EntityType: EntitySet 'tRaceTypes' is based on type 'tRaceType' that has no keys defined.\r\ntReligions: EntityType: EntitySet 'tReligions' is based on type 'tReligion' that has no keys defined.\r\n"}
4

1 回答 1

1

尝试这种方式,而不是担心伪造上下文,伪造存储库。

真正的存储库将是这样的:

public class CongressRepository : ICongressRepository
{
    ICongressContext db;
    CongressDb_DevEntities realDb = new CongressDb_DevEntities();

    public CongressRepository()
    {
        this.db = new CongressContext();
    }
    public CongressRepository(ICongressContext context)
    {
        this.db = context;
    }

    public List<tMember> GetMembers()
    {
        realDb.Configuration.LazyLoadingEnabled = false;
        var members = realDb.tMembers.Where(x => x.MembersID < 100).ToList();
        return members;
    }
}

假存储库将是这样的:

public class MockCongressRepository : ICongressRepository
{
    public List<tMember> GetMembers()
    {
         var members = new List<tMembers>{ new tMember { id = 1 } };
         return members;
    }
}

像这样的单元测试:

var controller = new CongressController(new MockCongressRepository());

使用最小起订量进行单元测试:

    var mockCongressRepository = new Mock<ICongressRepository>();
    mockCongressReposiory.Setup(mcr => mcr.GetMembers())
                         .Returns(new List<tMembers>{ new tMember { id = 1 } });        
    var controller = new CongressController(mockCongressRepository.Object);

注意:这里我们去掉了MockCongressRepository类,mock 是在单元测试附近定义的。

于 2013-10-30T23:07:44.053 回答