public void TestJsonResult_T_should_work_as_expected() { ApiControllerHttpActionResultActionTester actionTester = _controllerTester.Action(x => () => x.HttpActionResultActionWithArgument("test")); _controller.HttpActionResult = new JsonResult <TestClass>( new TestClass(), new JsonSerializerSettings(), System.Text.Encoding.UTF8, new HttpRequestMessage()); JsonResult <TestClass> result = null; JsonResult <TestClass> validatedResult = null; bool validateWasCalled = false; Action action = () => { result = actionTester.TestJsonResult <TestClass>(r => { validatedResult = r; validateWasCalled = true; }); }; action.Should().NotThrow(); result.Should().Be(_controller.HttpActionResult); validateWasCalled.Should().BeTrue(); validatedResult.Should().Be(_controller.HttpActionResult); _controller.HttpActionResult = _okResult; action.Should().Throw <ControllerTestException>().WithMessage( $"Expected IHttpActionResult type {result.GetType().FullName}. Actual: System.Web.Http.Results.OkResult."); }
public void Reject_BT_DIR_Confirm_ExistingBTAndValidCommentConcurency_CanReject() { //Arrange BusinessTrip businessTrip = mock.Object.BusinessTrips.Where(b => b.BusinessTripID == 9).FirstOrDefault(); businessTrip.RejectComment = "BT is too expensive"; Employee author = mock.Object.Employees.Where(e => e.EID == controller.ControllerContext.HttpContext.User.Identity.Name).FirstOrDefault(); List <BusinessTrip> selectedBusinessTripsList = new List <BusinessTrip>(); mock.Setup(m => m.SaveBusinessTrip(It.IsAny <BusinessTrip>())).Throws(new DbUpdateConcurrencyException()); selectedBusinessTripsList.Add(businessTrip); // Act JsonResult result = (JsonResult)controller.Reject_BT_DIR_Confirm(businessTrip); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target; //Assert Assert.IsInstanceOf(typeof(JsonResult), result); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual(modelError, data); mock.Verify(m => m.SaveBusinessTrip(businessTrip), Times.Once); messengerMock.Verify(m => m.Notify(It.Is <IMessage>(msg => msg.messageType.Equals(MessageType.DIRRejectsConfirmedToADM))), Times.Never); messengerMock.Verify(m => m.Notify(It.Is <IMessage>(msg => msg.messageType.Equals(MessageType.DIRRejectsConfirmedToADM))), Times.Never); messengerMock.Verify(m => m.Notify(It.Is <IMessage>(msg => msg.messageType.Equals(MessageType.DIRRejectsConfirmedToResponsible))), Times.Never); //messengerMock.Verify(m => m.Notify(It.Is<IMessage>(msg => msg.messageType.Equals(MessageType.DIRRejectsConfirmedToACC))), Times.Never); Assert.AreEqual("BT is too expensive", businessTrip.RejectComment); Assert.AreEqual((BTStatus.Planned | BTStatus.Modified), businessTrip.Status); }
public void PostEdit_NotValidModelSelectedDepartmentRAAA5_ViewEdit() { // Arrange EmployeeController controller = new EmployeeController(mock.Object); controller.ModelState.AddModelError("DateEmployed", "Field DateEmployed must be not null"); Employee employee = new Employee { EmployeeID = 1, FirstName = "Anastasia", LastName = "Zarose", DepartmentID = 1, DateDismissed = new DateTime(11 / 01 / 2013), DateEmployed = new DateTime(11 / 02 / 2011), IsManager = false }; string selectedDepartment = "RAAA5"; var expectedDepartmentList = (from m in mock.Object.Departments select m).ToList(); MvcApplication.JSDatePattern = "dd.mm.yy"; // Act JsonResult result = (JsonResult)controller.Edit(employee, null, selectedDepartment); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target; // Assert mock.Verify(m => m.SaveEmployee(It.IsAny <Employee>()), Times.Never); Assert.IsTrue(result != null); //Assert.IsTrue(((ViewResult)result).ViewName == ""); //Assert.AreEqual("dd.mm.yy", ((ViewResult)result).ViewBag.JSDatePattern); //Assert.AreEqual(expectedDepartmentList, ((ViewResult)result).ViewBag.DepartmentList); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual("", data); }
public void GetAllCustomers() { var _controller = new CustomerController(_context); var _response = _controller.All(); Assert.IsNotNull(_response, "Issue with customer"); var _result = new JsonResult(_response); Assert.IsInstanceOfType(_response, _result.GetType()); }
public void ValidUserNameandpassword() { var _login = new Login() { UserName = "******", Password = "******" }; var _controller = new AccountController(_config, _context); var _response = _controller.Authenticate(_login); Assert.IsNotNull(_response, "Issue with authentication"); var _result = new JsonResult(new OkResult()); Assert.IsInstanceOfType(_response, _result.GetType()); }
public void EditDepartment_ValidModelComcurrency_JsonErrorReturned() { //Arrange mock.Setup(m => m.SaveDepartment(It.IsAny <Department>())).Throws(new DbUpdateConcurrencyException()); DepartmentController target = new DepartmentController(mock.Object); //Act JsonResult result = (JsonResult)target.Edit(mock.Object.Departments.FirstOrDefault()); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target; //Assert mock.Verify(d => d.SaveDepartment(It.IsAny <Department>()), Times.Once()); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual(modelError, data); }
public void EditDatePost_ValidModelConcurrency_ErrorReturned() { //Arrange mock.Setup(m => m.SavePassport(It.IsAny <Passport>())).Throws(new DbUpdateConcurrencyException()); string modelError = "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled."; //Act JsonResult result = (JsonResult)controller.EditDate(mock.Object.Passports.FirstOrDefault()); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target; //Assert mock.Verify(d => d.SavePassport(It.IsAny <Passport>()), Times.Once()); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual(modelError, data); }
public void TestAgendaTodosJson() { JsonResult esperado = new JsonResult(); List <object> resultados = new List <object>(); this.Login("superusuario"); resultados.Add(controller.Academicas("2015-02-02T08:55", "2015-02-02T08:55")); resultados.Add(controller.Certificacoes("2015-02-02T08:55", "2015-02-02T08:55")); resultados.Add(controller.Reposicoes("2015-02-02T08:55", "2015-02-02T08:55")); resultados.Add(controller.Horarios("2015-02-02T08:55", "2015-02-02T08:55")); resultados.Add(controller.Conflitos("2015-02-02T08:55", "2015-02-02T08:55")); foreach (var resultado in resultados) { Assert.AreEqual(esperado.GetType(), resultado.GetType()); } }
public void Should_Added_Expense_Should_Return_Json() { var expenseInput = new ExpenseInputModel() { Id = 10, Payee = "Amazon", Amount = 18.00M, Category = "Stuff I Forget to Budget For", Account = "Middleburg", Date = DateTime.Today, Repeat = false, Impulse = true, Memo = "Dry Erase Paper Sheets", ColorCode = "Blue" }; var post = _sut.Create(expenseInput); var expectedType = new JsonResult(expenseInput); var result = post as JsonResult; Assert.IsInstanceOf(expectedType.GetType(), result); }
public void EditDepartment_InValidModel_NotSave() { //Arrange Mock <IRepository> mRepository = new Mock <IRepository>(); mRepository.Setup(d => d.Departments).Returns(new Department[] { new Department { DepartmentID = 1, DepartmentName = "EPUA" } }.AsQueryable()); DepartmentController target = new DepartmentController(mRepository.Object); Department department = new Department(); target.ModelState.AddModelError("error", "error"); //Act JsonResult result = (JsonResult)target.Edit(department); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target; //Assert mRepository.Verify(d => d.SaveDepartment(It.IsAny <Department>()), Times.Never()); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual("", data); }
private static void serialize(JsonResult result, int count) { string s = ""; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(result.GetType()); Console.WriteLine("序列化开始({0}),单位毫秒", count); System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch(); Console.WriteLine("Fastest.Json"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { JsonString stream = new JsonString(defaultSize); if (isInfo) { ShiboSerializer.Serialize(stream, result, info, sets); } else { ShiboSerializer.Serialize(stream, result, sets); } s = stream.ToString(); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); Console.WriteLine("\r\nServiceStack.Text"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = ServiceStack.Text.JsonSerializer.SerializeToString <JsonResult>(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); Console.WriteLine("\r\nNewtonsoft.Json"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = Newtonsoft.Json.JsonConvert.SerializeObject(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); //Console.WriteLine("\r\nJavaScriptSerializer"); //JavaScriptSerializer serializer = new JavaScriptSerializer(); //for (int i = 0; i < 5; i++) //{ // w.Start(); // for (int j = 0; j < count; j++) // { // s = serializer.Serialize(result); // } // w.Stop(); // Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); // w.Reset(); //} //Console.WriteLine(s); Console.WriteLine("\r\nDataContractJsonSerializer"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = JsonSerializer(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); }