public void AddVote_Should_Call_Command_Bus_When_Api_Called() { //Arrange var command = CommandFactory.BuildACommandOfType().AddVote; //Act _controller.AddVote(command); //Assert _commandBus.Verify(x => x.Dispatch(command)); }
public void QuestionController_AddVote_Should_Return_Error() { //Arrange List <Vote> myVotes = new List <Vote>() { new Vote() { QuestionId = 0 } }; ApplicationUser user = new ApplicationUser() { UserName = "******", MyVotes = myVotes }; 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.AddVoteToOption(It.IsAny <int>(), It.IsAny <int>(), 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.AddVote(0, 0); //Assert Assert.AreEqual("error", result.Data); }