日期:2014-05-20  浏览次数:21152 次

Mock 的问题。会MOCK 的同学进来帮我看看
先贴代码。代码有精简。只贴出相关的代码


C# code


    public interface IUnitOfWork
    {
        int SaveChanges();
    }


 public class UnitOfWork : IUnitOfWork
    {
       
public int SaveChanges()
        {
                return TaskDBContext.SaveChanges();
           }
}


    public interface IUserInfoRepository : IRepository<UserInfo>
    {
    }

 public class UserInfoRepository : RepositoryBase<UserInfo>, IUserInfoRepository
    {
        public UserInfoRepository(IDatabaseFactory dataBaseFactory)
            : base(dataBaseFactory)
        {

        }
    }

    public interface IUserInfoServices
    {
        bool UserInfo_Insert(UserInfo userInfo);
    }

   public class UserInfoServices : IUserInfoServices
    {

        private readonly IUserInfoRepository userInfoRepository;
        private readonly IUnitOfWork unitOfWork;

        public UserInfoServices(IUserInfoRepository userinfoRepository, IUnitOfWork unitOfWork)
        {
            this.userInfoRepository = userinfoRepository;
            this.unitOfWork = unitOfWork;
        }

        public bool UserInfo_Insert(UserInfo userInfo)
        {
            userInfoRepository.Add(userInfo);
            return unitOfWork.SaveChanges() == 1;
        }

}





主要的代码就是如上面的的
熟悉 Repository 和 UnitOfWork 的朋友一定很容易明白的。

好了。
我现在要通过 MOCK MOQ 来测试 bool UserInfo_Insert(UserInfo userInfo)

我的相关测试代码如下:
C# code


   public static IUnitOfWork MockUnitOfWork(List<UserInfo> userInfo)
        {
            var mockUnitOfWork = new Mock<IUnitOfWork>();
            mockUnitOfWork.Setup(x => x.SaveChanges()).Callback<UserInfo>((s) => userInfo.Add(s));
            return mockUnitOfWork.Object;
        }

        /// <summary>
        ///UserInfo_Insert 的测试
        ///</summary>
        [TestMethod()]
        public void UserInfo_InsertTest()
        {
            UserInfo userInfo = new UserInfo() { UserId = 1, UserName = "Test", Pwd = "oooooo", Email = "email@email.com" };

            List<UserInfo> userInfos = new List<UserInfo>();
            int userCount = userInfos.Count;

            var userInfoRepositoryMock = new Mock<IUserInfoRepository>();
            var unitOfWorkMock = MockUnitOfWork(userInfos);
            var userInfoServicesMock = new Mock<IUserInfoServices>();

            var userInfoServices = new UserInfoServices(userInfoRepositoryMock.Object, unitOfWorkMock);
            userInfoServicesMock.Setup(o => o.UserInfo_Insert(It.IsAny<UserInfo>())).Returns(true);

            userInfoServices.UserInfo_Insert(userInfo);

            Assert.AreEqual(userInfos.Count, 2);

        }






问题:

我在调试的时候。它报错。

 
C# code


mockUnitOfWork.Setup(x => x.SaveChanges()).Callback<UserInfo>((s) => userInfo.Add(s));




Invalid callback. Setup on method with parameters () cannot invoke callback with parameters (UserInfo)


我查了半天。我也没有找出来是什么地方有问题。
希望会 MOCK 的朋友能帮我看看。
我的QQ 2908 36527


------解决方案--------------------
http://cncc.bingj.com/cache.aspx?q=invoke+callback+with+parameters+&d=5038151595067043&mkt=zh-CN&setlang=zh-CN&w=babe3078,27c50a0a