如何利用Moq框架来模拟我的存储库? [英] How Can I Make Use Of Moq Framework To Mock My Repositories?

查看:113
本文介绍了如何利用Moq框架来模拟我的存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够创建模拟对象并成功对模拟实体进行更新测试。

但问题在于创建测试。这是我的示例代码。

总是我的 testPG 为空,因此测试失败



基类:

I'm able to create mock objects and do a update test with mocked entities successfully.
But the issue is with create test. Here is my sample code.
In this always my testPG is null and so the test fails

base class:

public class TestContext where T : class
   {

       protected Mock<t> mockRepository;
       protected Mock<testentities> mockTestEntities;

       protected TestContext()
       {
           mockRepository = new Mock<t>(MockBehavior.Strict);
           mockTestEntities= new Mock<testentities>(MockBehavior.Strict);
           mockTestEntities.Setup(ase => ase.SaveChanges()).Returns(1);
      }


public class PGRepositoryTest : TestContext<ipgrepository>
    {


 [TestInitialize]
        public void TestInitialize()
        {
            pgRepository = new PGRepository(mockTestEntities.Object);
                
          pgViewModel = new PGViewModel
                                {   ID = 1,
                                    Name = "PG Unit Test",
                                    DisplayName = "PG Unit Test",
                                    IsActive = true,                                    
                                };
            sessionViewModel = pgViewModel;

            userContext = new UserContext
                                {
                                    UserID = "XXXX"
                                };
}


 [TestMethod]
        public void CreateTest()
        {
            
            var IdSet = new IDSet();
            
            IdSet = pgRepository.Create(pgViewModel, sessionViewModel, userContext);
            
            PresentationGroupViewModel testPG = pgRepository.GetPresentationGroup(IdSet.ID);              

              Assert.AreEqual(IdSet.ID, testPG.ID);

        }
}</ipgrepository></testentities></t></testentities></t>

推荐答案

这篇关于如何利用Moq框架来模拟我的存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆