【问题标题】:MsTest : Actual value increases everytime test method runsMsTest :每次测试方法运行时实际值都会增加
【发布时间】:2018-08-15 08:56:37
【问题描述】:

这是我的测试课

[TestClass]
public class FooServiceTest
{
    private IYourRepository _yourRepository;

    [TestInitialize]
    public void Initialize()
    {
        _yourRepository = new Mock<YourRepository>().Object;

    }

    [TestMethod]
    public void GetPushableEntries_gets_all_pushable_entries()
    {
        var yourObjectList = new List<YourObject>
        {
            new WaitingQueue
            {                   
                ProfileId = 26,
                IsDeleted = false,
                Pushable = true
            },
            new WaitingQueue
            {                    
                ProfileId = 27,
                IsDeleted = false,
                Pushable = true
            },
            new WaitingQueue
            {                   
                ProfileId = 28,
                IsDeleted = false,
                Pushable = false
            }

        };

        foreach (var yourObject in yourObjectList)
        {
            _yourRepository.Create(yourObject);
        }

        var pushableEntries = _yourRepository.GetList(x => x.Pushable);
        pushableEntries.Count.ShouldEqual(2);
        pushableEntries.ShouldNotBeNull();
        pushableEntries.ShouldBe<IReadOnlyCollection<WaitingQueue>>();

    }

}

这里是ShouldEqual方法

public static T ShouldEqual<T>(this T actual, object expected)
{
    Assert.AreEqual(expected, actual);
    return actual;
}

这里是GetList 方法

public IReadOnlyCollection<T> GetList(Expression<Func<T, bool>> @where, params Expression<Func<T, object>>[] nav)
{
    using (var dbContext = new MyDbContext())
    {
        return GetFiltered(dbContext, nav).Where(where).ToList();
    }
}

每次我运行GetPushableQueues_gets_all_pushable_entries()方法

实际值增加2。

Assert.AreEqual failed. Expected:<2>. Actual:<2>. //first run
Assert.AreEqual failed. Expected:<2>. Actual:<4>. //second run
Assert.AreEqual failed. Expected:<2>. Actual:<6>. //third run

即使我清理测试项目并重建它,这个问题仍然存在。 知道为什么会发生这种情况以及我错过了什么吗?

注意:还有其他测试方法使用_yourRepository并调用Create方法来创建实体。

【问题讨论】:

    标签: c# unit-testing mocking moq mstest


    【解决方案1】:

    问题是您实际上在那里使用了某种存储库。 你不要嘲笑它。 _yourRepository = new Mock().Object;

    应该是 _yourRepository = new Mock().Object;

    您在 IYourRepository 接口中使用的所有方法也应该被模拟/设置。

    【讨论】:

    • 嗯,我也应该模拟“IYourRepository”的所有方法吗?并且模拟实现 IYourRepository 的类不起作用?
    • 不,模拟类不起作用。您不需要全部模拟它们,只需模拟您在特定测试中使用的那些。
    • 你的意思是模拟特定的方法?我是这个测试的新手。我的应用程序的设置方式是有一个由 ISomeRepository 继承的通用存储库,而 ISomeService 继承并扩展了 ISomeRepository。我想测试 SomeService 类中的方法。所以根据你说的,我应该只从 ISomeRepository 模拟我需要的方法,然后用它来测试我需要的 SomeService 方法?
    • 请注意“using (var dbContext = new MyDbContext())”这一行。当有 dbcontext 时,您不会模拟任何东西。它连接到真实的数据库(除非您配置了内存数据库)。
    • 是的,只设置“_repository.GetList”方法。应该够了。
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多