使用计数方法

Using count method

我对 Moq Framework 和 unitests 还是个新手。我的目标是向模拟对象插入一些信息,并希望能够计算使用当前值插入的对象的数量。

我通过以下方式构建模拟:

var mockCpmSqlDbContext = new Mock<CpmSqlDbContext>();
var mockSetPolicies = new Mock<DbSet<Policies>>();

var dataStore = new List<Policies>
{
    new Policies
    {
        PolicyID = 1,
        PolicyName = "policy1",
        PolicyDesc = "policy1desc"
    },
    new Policies
    {
        PolicyID = 2,
        PolicyName = "policy2",
        PolicyDesc = "policy2desc"
    },
};
var policyData = dataStore.AsQueryable();

mockSetPolicies.As<IQueryable<Policies>>().
    Setup(m => m.Provider).Returns(policyData.Provider);
mockSetPolicies.As<IQueryable<Policies>>().
    Setup(m => m.Expression).Returns(policyData.Expression);
mockSetPolicies.As<IQueryable<Policies>>().
    Setup(m => m.ElementType).Returns(policyData.ElementType);
mockSetPolicies.As<IQueryable<Policies>>().
    Setup(m => m.GetEnumerator()).Returns(policyData.GetEnumerator());


mockSetPolicies.Setup(x => x.Create<Policies>()).Returns(() => new Policies());
mockSetPolicies.Setup(x => x.Add(It.IsAny<Policies>())).Callback<Policies>(dataStore.Add);
mockSetPolicies.As<IQueryable<Policies>>().Setup(m => m.GetEnumerator()).Returns(() => policyData.GetEnumerator());

mockSetPolicies.Setup(x => x.Find(It.IsAny<Policies>()));

mockCpmSqlDbContext.Setup(m => m.Policies).Returns(mockSetPolicies.Object);
var service = new MSsqlProvider(mockCpmSqlDbContext.Object);

我的模拟上下文中目前有两个策略。 我想再添加两个具有相同名称 "policy2" 的策略,因此添加后我将拥有三个具有策略名称 "policy2".

的对象
service.AddPolicy(new Policies {PolicyID = 3, PolicyName = "policy2", PolicyDesc = "pDesc3"});
service.AddPolicy(new Policies { PolicyID = 4, PolicyName = "policy2", PolicyDesc = "pDesc3" });

我的添加政策功能:

public void AddPolicy(Policies policy)
{
    _dbEntities.Policies.Add(policy);
    _dbEntities.SaveChanges();
}

检查断言的代码:

//Assert
var expected = 3;
//first, can I know how many object I have the policy name "policy2".
var actual = mockSetPolicies.Object.Count(p => p.PolicyName == "policy2");
//second, can I assert the number of objects I have currently with the name "policy2".
Assert.AreEqual(expected, actual);

此代码当前有效

有一个对象来保存数据

var dataStore = new List<Policies>
{
    new Policies
    {
        PolicyID = 1,
        PolicyName = "policy1",
        PolicyDesc = "policy1desc"
    },
    new Policies
    {
        PolicyID = 2,
        PolicyName = "policy2",
        PolicyDesc = "policy2desc"
    },
};

var policyData = dataStore.AsQueryable();

设置模拟以将记录插入到您的数据存储中。

mockSetPolicies
    .Setup(x => x.Add(It.IsAny<Policies>()))
    .Callback<Policies>(dataStore.Add);

这就是你所缺少的。

从那里你应该能够使用像 Count() 这样的 linq 查询,它会枚举模拟数据库集。

您还需要进行一些小的修改以允许有效枚举 dbset。

//Change this to use a Func delegate to allow multiple calls.
mockSetPolicies.As<IQueryable<Policies>>()
    .Setup(m => m.GetEnumerator())
    .Returns(() => policyData.GetEnumerator());

//Set this up to return a new model if it is being used.
mockSetPolicies
    .Setup(x => x.Create<Policies>())
    .Returns(() => new Policies());

假设该服务公开了一个允许访问 dbset 的 属性,您可以在那里或通过模拟它自己断言计数。

//Act
service.AddPolicy(new Policies {PolicyID = 3, PolicyName = "policy2", PolicyDesc = "pDesc3"});
service.AddPolicy(new Policies { PolicyID = 4, PolicyName = "policy2", PolicyDesc = "pDesc3" });
///Assert
var expected = 3;
//first, can I know how many object I have the policy name "policy2".
var actual = mockSetPolicies.Object.Count(p => p.PolicyName == "policy2");
//second, can I assert the number of objects I have currently with the name "policy2".
Assert.AreEqual(expected, actual);