private Mock <DbSet <T> > SetDatabase <T>(IQueryable <T> data) where T : class { var mock = new Moq.Mock <DbSet <T> >(); mock.As <IQueryable <T> >().Setup(m => m.Provider).Returns(data.Provider); mock.As <IQueryable <T> >().Setup(m => m.Expression).Returns(data.Expression); mock.As <IQueryable <T> >().Setup(m => m.ElementType).Returns(data.ElementType); mock.As <IQueryable <T> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); return(mock); }
public void ListadoExpedienteConPedimentoInexistenteRegresaError() { //Arrange //ClaimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity().) Moq.Mock <IIdentityExtensions> mockI = new Moq.Mock <IIdentityExtensions>(); mockI.Setup(s => s.GetClientId(It.IsAny <IIdentity>())).Returns("590"); mockI.Setup(s => s.GetNombreCliente(It.IsAny <IIdentity>())).Returns("DANFOSS INDUSTRIES S. DE R. L."); IdentityExtensions.Implementation = mockI.Object; Moq.Mock <ClaimsPrincipal> mockP = new Moq.Mock <ClaimsPrincipal>(); mockP.SetupGet(p => p.Identity.Name).Returns("*****@*****.**"); mockP.Setup(p => p.IsInRole("Administrador")).Returns(false); mockP.Setup(p => p.FindFirst("ClientId")).Returns(new Claim("ClientId", "590")); mockP.As <IPrincipal>(); IPrincipal unmockedIPrincipal = mockP.Object; //((ClaimsIdentity)unmockedIPrincipal.Identity).AddClaim(new Claim("ClientId", "590")); Moq.Mock <ControllerContext> mockContext = new Moq.Mock <ControllerContext>(); mockContext.SetupGet(p => p.HttpContext.User).Returns(unmockedIPrincipal); mockContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new InternetReports.Areas.Operacion.Controllers.DescargaExpedientesController(); controller.ControllerContext = mockContext.Object; controller._catalogos = new InternetReports.Models.FakeARCHIVOS_ALT(); var pedimento = new InternetReports.Models.PedimentosSaaiSirView() { Referencia = "1900024-00", Pedimento = "9000751", Aduana = "81", Seccion = "0", IdClienteC = 590, IdClienteP = 590, FechaPago = new DateTime(2019, 1, 10) }; controller._catalogos.PedimentosSaaiSirViews.Add(pedimento); controller._catalogos.SaveChanges(); BusquedaExpedienteViewModel busquedaViewModel = new BusquedaExpedienteViewModel() { Pedimento = "9000754", Aduana = "810", Año = "2019" }; //Act ViewResult result = controller.ListadoExpediente(busquedaViewModel).Result as ViewResult; //Assert Assert.AreEqual("Index", result.ViewName); Assert.IsTrue(result.ViewData.ModelState.ContainsKey("PedimentoNoEncontrado")); Assert.IsNotNull(result.ViewData.ModelState["PedimentoNoEncontrado"].Errors); Assert.IsNotNull(result.ViewData.ModelState["PedimentoNoEncontrado"].Errors.Contains(new ModelError("No se encontró el pedimento o la referencia requerida"))); }
public void Test_CorrectTemplateIsUsed_LateLunch_ExplicitInterface(string currentTime, string expectedTemplate) { // // Create mocks: // var loggerMock = new Moq.Mock <ILogger>(); var bobMock = new Moq.Mock <IEmployee>(); bobMock.Setup(x => x.IsWorkingOnDate(It.IsAny <DateTime>())) .Returns(true); var employeeServiceMock = new Moq.Mock <IEmployeeService>(); employeeServiceMock.Setup(x => x.GetEmployeesInNewYorkOffice()) .Returns(new[] { bobMock.Object }); var notificationServiceMock = new Moq.Mock <INotificationService>(); // // Create instance of class I'm testing: // var classUnderTest = new Moq.Mock <LunchNotifier_SeamAndExplicitInterface>(notificationServiceMock.Object, employeeServiceMock.Object, loggerMock.Object) { CallBase = true }; classUnderTest.As <IDateTimeOverridable>().Setup(x => x.GetDateTime()) .Returns(DateTime.Parse(currentTime)); // // Run some logic to test: // classUnderTest.Object.SendLunchtimeNotifications(); // // Check the results: // notificationServiceMock.Verify(x => x.SendEmail(It.IsAny <IEmployee>(), expectedTemplate)); }