コード例 #1
0
        public void TestingMock()
        {
            //create a list of review
            var r1 = new Review();

            r1.courseId    = 1;
            r1.description = "cool cool";


            var r2 = new Review();

            r2.courseId    = -1;
            r2.description = "super hard";


            var r3 = new Review();

            r3.courseId    = 3;
            r3.description = "very easy";


            var r4 = new Review();

            r4.courseId    = 4;
            r4.description = "ruined my life";


            var reviews = new List <Review> {
                r1, r2, r3, r4,
            }.AsQueryable();

            var reviewMock = new Mock <DbSet <Review> >();

            reviewMock.As <IQueryable <Review> >().Setup(m => m.Provider).Returns(reviews.Provider);
            reviewMock.As <IQueryable <Review> >().Setup(m => m.Expression).Returns(reviews.Expression);
            reviewMock.As <IQueryable <Review> >().Setup(m => m.ElementType).Returns(reviews.ElementType);
            reviewMock.As <IQueryable <Review> >().Setup(m => m.GetEnumerator()).Returns(reviews.GetEnumerator());

            var mockedContext = new Mock <IDbContext>();

            mockedContext.Setup(p => p.Review).Returns(reviewMock.Object);

            var test = mockedContext.Object;


            var hope = test.Review;

            var c = hope.Count <Review>();

            var controller = new ReviewController(test);

            var tmpDontDelete = controller.Report(3);             // for code coverage
            var result        = controller.FilterBy(1) as ViewResult;

            var viewData = result.ViewData;


            var model = (IEnumerable <RateMyClasses.Models.Review>)result.Model;


            var modelList = model.ToList <Review>();

            Assert.True(model.Count <Review>() == 1);

            Assert.True(modelList.First <Review>().description == "cool cool");
            //var m = result.Model;

            //var test2 = viewData.Model;

            Assert.Equal("cool cool", modelList.First <Review>().description);
            //Assert.Equal(!viewData["Result"].Equals("example"));
        }