Example #1
0
 public void Test4()
 {
     try
     {
         var             res  = new Mock <admin>(context);
         AdminController h2   = new AdminController(res.Object);
         Book            book = new Book {
             Book_Id = 11, Book_Name = "Biology", Author = "S. Chand", Language = "English", No_of_pages = 500, Price = 400
         };
         var d1 = h2.AddBook(book);
         var R1 = d1 as OkObjectResult;
         Assert.AreEqual(200, R1.StatusCode);
     }
     catch (Exception e)
     {
         Assert.AreEqual("Object reference not set to an instance of an object.", e.Message);
     }
 }
Example #2
0
        public void AddBook()
        {
            // Arrange
            var managerMock = new Mock <IBookManager>();

            managerMock.Setup(x => x.AddBook(It.IsAny <Book>()))
            .Callback <Book>((book) => AddTestBook(book));

            var logger = new Mock <ILogger <AdminController> >();

            AdminController controller = new AdminController(managerMock.Object, logger.Object);

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = new ClaimsPrincipal(new ClaimsIdentity(
                                                   new Claim[] { new Claim(ClaimTypes.Name, "testAdmin"), new Claim(ClaimTypes.Role, "Admin") },
                                                   "authTypeName")
                                               )
                }
            };

            var book = new Book {
                Name = "D book", Text = "D text", Price = 1
            };


            // Act
            var result = controller.AddBook(book);

            // Assert
            Assert.NotNull(result);

            var okObjectResult = result as OkObjectResult;

            Assert.NotNull(okObjectResult);
        }