模拟DbContext - 无法插入新对象

时间:2015-03-10 11:59:06

标签: c# entity-framework moq

https://msdn.microsoft.com/en-us/data/dn314429.aspx

我正在尝试使用类似于上面链接的查询方案的代码。 EF 6.1 + Moq。 它可以正常使用这样的预填充数据(我可以毫无问题地查询它们):

_context = new Mock<MyContext>();

IQueryable<users> users = new List<users>
        {
            new users{id = 1, email = "test@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now},
            new users{id = 2, email = "test2@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now},
        }.AsQueryable();
var mockUsers = new Mock<DbSet<users>>();
        mockUsers.As<IQueryable<users>>().Setup(m => m.Provider).Returns(users.Provider);
        mockUsers.As<IQueryable<users>>().Setup(m => m.Expression).Returns(users.Expression);
        mockUsers.As<IQueryable<users>>().Setup(m => m.ElementType).Returns(users.ElementType);
        mockUsers.As<IQueryable<users>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

_context.Setup(m => m.users).Returns(mockUsers.Object);

但是当我尝试在我的测试方法中添加新对象时,它不起作用:

[TestMethod]
    public void AddUser()
    {
        _context.Object.users.Add(new users { email = "test3@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now });
        _context.Object.SaveChanges();
        var count = _context.Object.users.Count();
        Assert.AreEqual(3, count); // count == 2 instead 3
    }

是否可以修改它,所以我的模拟上下文实际上会应用SaveChanges?我是否正确思考,或者出于某种原因这种做法是错误的?

如果我使用非查询方法,我将无法使用:

mockContext.Object.users.Count()

假设我有方法进行测试,片段如下:

 context.cards.Add(card);
 context.SaveChanges();
 SetCardForUser(card.id, user);

 protected void SetCardForUser(long cardId, users user)
    {
        user.card_id = cardId;
        context.SaveChanges();
    }

我需要在将对象插入数据库后返回card.id。 我应该严格遵守SRP并且只有一种方法可以做到吗?

我如何测试此方法只有AddCardAndStartAuthCharge是公共的并且其他方法受到保护? :

public async Task<long> AddCardAndStartAuthCharge(CardModel model, string username)
    {
        var hash = await GetHashForCardAuth(model);
        var CardWithId = AddCardToDatabase(model, username);
        long chargeId = AddHashToCard(CardWithId, hash);
        return chargeId;
    }

3 个答案:

答案 0 :(得分:0)

正如评论中所提到的,在使用EntityFramework时,绝对不需要自己管理ID。请观察以下代码段:

MyContext context = new MyContext();
User user = new User() { Name = "Name1" };
context.Users.Add(user);
context.SaveChanges();
Console.WriteLine(user.Id);

执行SaveChanges后会自动获得一个ID。因此,像您的其他问题中的那些片段可以很容易地重构。

//Old
public CardModel AddCard(CardModel model, string username)
{
    var user = context.users.Where(x => x.email.ToLower() == username.ToLower()).First();
    var card = new cards()
    {
        created = DateTime.Now,
        modified = DateTime.Now,
        name = model.Name,
        type = model.CardType,
        user_id = user.id
    };
    context.cards.Add(card);
    context.SaveChanges();
    model.Id = card.id;
    return model;
}

//New
public CardModel AddCard(CardModel model, string username)
{
    var user = context.users.Where(x => x.email.ToLower() == username.ToLower()).First();
    user.Cards.Add(model)
    user.SaveOrUpdate()
    return model;
}

观察到{@ 1}}之类的东西绝对不可能!

如果不对现有模型进行一些更改,这很可能无效。您必须配置EntityFramework以将模型中的更改正确级联到所有子模型(例如,如果删除了model.Id = card.id,则删除所有User)。

现在,针对您的问题。如果您根据上述内容重构代码,则永远不会依赖上下文中包含的CardsSaveChanges()来提供模型。

由于您使用的是模拟,ID也不会发生变化,但鉴于您的代码,您不必明确使用任何ID。

您已经拥有的模拟就足够了,因为您无需从上述上下文中检索刚刚添加到上下文中的模型以便使用它。牢记这一点,既没有必要嘲笑DBSet的后果,也没有必要模仿像SaveChanges这样的事情。

答案 1 :(得分:0)

是的,可以使用模拟保存方法。请看这个:

IQualityIssuesRepository qir = new QualityIssuesRepository();
        Mock<IQualityIssuesRepository> qualityIssuesRepository = new Mock<IQualityIssuesRepository>();
qualityIssuesRepository.CallBase = true;
Action<List<int>, int> saveNotification = qir.SaveNotification;
qualityIssuesRepository.Setup(p => p.SaveNotification(It.IsAny<List<int>>(), It.IsAny<int>()))
            .Callback(saveNotification);

您应该在模拟的存储库中设置Save方法。只需创建该repo的实例,并将该方法用于模拟存储库。希望它有所帮助:)

答案 2 :(得分:0)

考虑在更高级别的抽象中进行模拟,然后使用您使用的工具进行紧密耦合。

也许您的视图模型应该依赖于服务而不是您使用的工具的详细信息(即IIsesServiceChannel)。

以下是一个例子:

我通常使用IServices,Services和MockServices。

  • IServices提供所有业务逻辑必须调用方法的可用操作。
  • 服务是我的代码隐藏注入视图模型(即实际数据库)的数据访问层。
  • MockServices是我的单元测试注入视图模型的数据访问层(即模拟数据)。

<强> IServices:

public interface IServices
{
    IEnumerable<Warehouse> LoadSupply(Lookup lookup);
    IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Lookup lookup);

    IEnumerable<Inventory> LoadParts(int daysFilter);
    Narration LoadNarration(string stockCode);
    IEnumerable<PurchaseHistory> LoadPurchaseHistory(string stockCode);

    IEnumerable<StockAlternative> LoadAlternativeStockCodes();
    AdditionalInfo GetSupplier(string stockCode);
}

<强> MockServices:

public class MockServices : IServices
{
    #region Constants
    const int DEFAULT_TIMELINE = 30;
    #endregion

    #region Singleton
    static MockServices _mockServices = null;

    private MockServices()
    {
    }

    public static MockServices Instance
    {
        get
        {
            if (_mockServices == null)
            {
                _mockServices = new MockServices();
            }

            return _mockServices;
        }
    }
    #endregion

    #region Members
    IEnumerable<Warehouse> _supply = null;
    IEnumerable<Demand> _demand = null;
    IEnumerable<StockAlternative> _stockAlternatives = null;
    IConfirmationInteraction _refreshConfirmationDialog = null;
    IConfirmationInteraction _extendedTimelineConfirmationDialog = null;
    #endregion

    #region Boot
    public MockServices(IEnumerable<Warehouse> supply, IEnumerable<Demand> demand, IEnumerable<StockAlternative> stockAlternatives, IConfirmationInteraction refreshConfirmationDialog, IConfirmationInteraction extendedTimelineConfirmationDialog)
    {
        _supply = supply;
        _demand = demand;
        _stockAlternatives = stockAlternatives;
        _refreshConfirmationDialog = refreshConfirmationDialog;
        _extendedTimelineConfirmationDialog = extendedTimelineConfirmationDialog;
    }

    public IEnumerable<StockAlternative> LoadAlternativeStockCodes()
    {
        return _stockAlternatives;
    }

    public IEnumerable<Warehouse> LoadSupply(Lookup lookup)
    {
        return _supply;
    }

    public IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Syspro.Business.Lookup lookup)
    {
        return _demand;
    }

    public IEnumerable<Inventory> LoadParts(int daysFilter)
    {
        var job1 = new Job() { Id = Globals.jobId1, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode100 };
        var job2 = new Job() { Id = Globals.jobId2, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode200 };
        var job3 = new Job() { Id = Globals.jobId3, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode300 };

        return new HashSet<Inventory>()
        {
            new Inventory() { StockCode = Globals.stockCode100, UnitQTYRequired = 1, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job1} },
            new Inventory() { StockCode = Globals.stockCode200, UnitQTYRequired = 2, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job2} },
            new Inventory() { StockCode = Globals.stockCode300, UnitQTYRequired = 3, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job3} },
        };
    }
    #endregion

    #region Selection
    public Narration LoadNarration(string stockCode)
    {
        return new Narration()
        {
            Text = "Some description"
        };
    }

    public IEnumerable<PurchaseHistory> LoadPurchaseHistory(string stockCode)
    {
        return new List<PurchaseHistory>();
    }

    public AdditionalInfo GetSupplier(string stockCode)
    {
        return new AdditionalInfo()
        {
            SupplierName = "Some supplier name"
        };
    }
    #endregion

    #region Creation
    public Inject Dependencies(IEnumerable<Warehouse> supply, IEnumerable<Demand> demand, IEnumerable<StockAlternative> stockAlternatives, IConfirmationInteraction refreshConfirmation = null, IConfirmationInteraction extendedTimelineConfirmation = null)
    {
        return new Inject()
        {
            Services = new MockServices(supply, demand, stockAlternatives, refreshConfirmation, extendedTimelineConfirmation),

            Lookup = new Lookup()
            {
                PartKeyToCachedParts = new Dictionary<string, Inventory>(),
                PartkeyToStockcode = new Dictionary<string, string>(),
                DaysRemainingToCompletedJobs = new Dictionary<int, HashSet<Job>>(),
.
.
.

            },

            DaysFilterDefault = DEFAULT_TIMELINE,
            FilterOnShortage = true,
            PartCache = null
        };
    }

    public List<StockAlternative> Alternatives()
    {
        var stockAlternatives = new List<StockAlternative>() { new StockAlternative() { StockCode = Globals.stockCode100, AlternativeStockcode = Globals.stockCode100Alt1 } };
        return stockAlternatives;
    }

    public List<Demand> Demand()
    {
        var demand = new List<Demand>()
        {
            new Demand(){ Job = new Job{ Id = Globals.jobId1, StockCode = Globals.stockCode100, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode100, RequiredQTY = 1}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId2, StockCode = Globals.stockCode200, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode200, RequiredQTY = 2}, 
        };
        return demand;
    }

    public List<Warehouse> Supply()
    {
        var supply = new List<Warehouse>() 
        { 
            Globals.Instance.warehouse1, 
            Globals.Instance.warehouse2, 
            Globals.Instance.warehouse3,
        };
        return supply;
    }
    #endregion
}

<强>服务

public class Services : IServices
{
    #region Singleton
    static Services services = null;

    private Services()
    {
    }

    public static Services Instance
    {
        get
        {
            if (services == null)
            {
                services = new Services();
            }

            return services;
        }
    }
    #endregion

    public IEnumerable<Inventory> LoadParts(int daysFilter)
    {
        return InventoryRepository.Instance.Get(daysFilter);
    }

    public IEnumerable<Warehouse> LoadSupply(Lookup lookup)
    {
        return SupplyRepository.Instance.Get(lookup);
    }

    public IEnumerable<StockAlternative> LoadAlternativeStockCodes()
    {
        return InventoryRepository.Instance.GetAlternatives();
    }

    public IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Lookup lookup)
    {
        return DemandRepository.Instance.Get(stockCodes, daysFilter, lookup);
    }
.
.
.

单元测试:

    [TestMethod]
    public void shortage_exists()
    {
        // Setup
        var supply = new List<Warehouse>() { Globals.Instance.warehouse1, Globals.Instance.warehouse2, Globals.Instance.warehouse3 };
        Globals.Instance.warehouse1.TotalQty = 1;
        Globals.Instance.warehouse2.TotalQty = 2;
        Globals.Instance.warehouse3.TotalQty = 3;

        var demand = new List<Demand>()
        {
            new Demand(){ Job = new Job{ Id = Globals.jobId1, StockCode = Globals.stockCode100, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode100, RequiredQTY = 1}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId2, StockCode = Globals.stockCode200, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode200, RequiredQTY = 3}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId3, StockCode = Globals.stockCode300, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode300, RequiredQTY = 4}, 
        };

        var alternatives = _mock.Alternatives();
        var dependencies = _mock.Dependencies(supply, demand, alternatives);

        var viewModel = new MainViewModel();
        viewModel.Register(dependencies);

        // Test
        viewModel.Load();

        AwaitCompletion(viewModel);

        // Verify
        var part100IsNotShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode100) && (!p.HasShortage)).Single() != null;
        var part200IsShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode200) && (p.HasShortage)).Single() != null;
        var part300IsShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode300) && (p.HasShortage)).Single() != null;

        Assert.AreEqual(true, part100IsNotShort &&
                                part200IsShort &&
                                part300IsShort);
    }

<强> CodeBehnd:

    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += (s, e) =>
            {
                this.viewModel = this.DataContext as MainViewModel;

                var dependencies = GetDependencies();
                this.viewModel.Register(dependencies);
.
.
.

<强>视图模型:

    public MyViewModel()
    {
.
.
.
    public void Register(Inject dependencies)
    {
        try
        {
            this.Injected = dependencies;

            this.Injected.RefreshConfirmation.RequestConfirmation += (message, caption) =>
                {
                    var result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
                    return result;
                };

            this.Injected.ExtendTimelineConfirmation.RequestConfirmation += (message, caption) =>
                {
                    var result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
                    return result;
                };

.
.
.
        }

        catch (Exception ex)
        {
            Debug.WriteLine(ex.GetBaseException().Message);
        }
    }