コード例 #1
0
        public void QuestionController_AddOption_Should_Return_Success()
        {
            //Arrange
            ApplicationUser user = new ApplicationUser()
            {
                UserName = "******"
            };
            var mockedProvider = new Mock <IEfSociumDataProvider>();

            mockedProvider.Setup(p => p.FindUserByUsername(It.IsAny <string>())).Returns(user);
            var mockedService = new Mock <IQuestionService>();

            mockedService.Setup(s => s.GetProvider).Returns(mockedProvider.Object);
            mockedService.Setup(s => s.AddOptionToQuestion(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()));
            var helper = new MapperHelper();

            var mockPrincipal = new Mock <IPrincipal>();

            mockPrincipal.SetupGet(p => p.Identity.Name).Returns("Pesho");
            mockPrincipal.Setup(p => p.IsInRole("User")).Returns(true);

            // create mock controller context
            var mockContext = new Mock <ControllerContext>();

            mockContext.SetupGet(p => p.HttpContext.User).Returns(mockPrincipal.Object);
            mockContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            QuestionController controller = new QuestionController(mockedService.Object, helper)
            {
                ControllerContext = mockContext.Object
            };

            //Act
            var result = controller.AddOption("test", 0);

            //Assert
            Assert.AreEqual("success", result.Data);
        }