Esempio n. 1
0
        public void When_Create_Return_Valid_Data()
        {
            //arrange
            var getAllCategoryResponse = new GetAllCategoryResponse();
            var categories             = new List <CategoryResponse>
            {
                new CategoryResponse
                {
                    Id   = 1,
                    Name = "Programming"
                },
                new CategoryResponse
                {
                    Id   = 1,
                    Name = "Programming"
                },
            };

            getAllCategoryResponse.Success = true;
            getAllCategoryResponse.Data    = categories;
            _categoryService.GetAllCategory().Returns(getAllCategoryResponse);
            var result     = _bookController.Create() as ViewResult;
            var resultView = result.Model as FormBookViewModel;

            //assert
            Assert.AreEqual(getAllCategoryResponse.Data.First().Id.ToString(), resultView.CategorySelectList.First().Value);
            Assert.AreEqual(getAllCategoryResponse.Data.First().Name, resultView.CategorySelectList.First().Text);
        }
Esempio n. 2
0
        public void CreatePostShould_ReturnViewIfBookServiceThrows()
        {
            // Arrange
            var fakeHttpContext = new Mock <HttpContextBase>();
            var fakeIdentity    = new GenericIdentity("User");
            var principal       = new GenericPrincipal(fakeIdentity, null);

            fakeHttpContext.Setup(t => t.User).Returns(principal);
            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);

            BookController controller = new BookController(
                booksServiceMock.Object,
                pagesServiceMock.Object,
                pageConnectionsServiceMock.Object,
                usersServiceMock.Object
                );

            controller.ControllerContext = controllerContext.Object;

            // Act
            usersServiceMock.Setup(x => x.FindSingle("User")).Returns(new User());
            booksServiceMock.Setup(x => x.Add(new Book())).Throws(new Exception());
            var result = controller.Create(new BookCreateViewModel());

            // Assert
            Assert.IsNotNull(result);
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            BookController bookController = new BookController();

            while (true)
            {
                Console.Write("Request>>");
                string request = Console.ReadLine();

                switch (request.ToLower())
                {
                case "single":
                    bookController.Single(1);
                    break;

                case "create":
                    bookController.Create();
                    break;

                case "update":
                    bookController.Update(1);
                    break;

                default:
                    Console.WriteLine("Error Syntax!!!");
                    break;
                }
            }
        }
Esempio n. 4
0
        public async Task Should_create_book()
        {
            BookController books = new BookController(new InMemoryDb(), new FakeDashboard());

            Assert.IsAssignableFrom <OkResult>(
                await books.Create(new FakeBook())
                );
        }
Esempio n. 5
0
        public async Task Should_remove_book()
        {
            BookController books = new BookController(new InMemoryDb(), new FakeDashboard());
            Guid           guid  = Guid.NewGuid();
            await books.Create(new FakeBook(guid));

            Assert.IsAssignableFrom <OkResult>(
                await books.RemoveBook(guid)
                );
        }
        public void Task_Create_Book_Return_OkResult()
        {
            //Arrange
            var controller = new BookController(context);

            //Act
            var data = controller.Create();

            //Assert
            Assert.IsType <ViewResult>(data);
        }
Esempio n. 7
0
        public void TestCreateView()
        {
            // Arrange
            BookController controller = new BookController();

            // Act
            ViewResult result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Esempio n. 8
0
        public async Task Should_add_to_dashboard(int quantity, Type expectedResult)
        {
            BookController books = new BookController(new InMemoryDb(), new FakeDashboard());
            Guid           guid  = Guid.NewGuid();
            await books.Create(new FakeBook(guid));

            Assert.IsAssignableFrom(
                expectedResult,
                await books.AddToDashboard(guid, quantity)
                );
        }
        public void Throw_ArgumentNullException_When_Passed_ViewModel_Is_Null()
        {
            // Arrange
            BookCreateViewModel  viewModelNull   = null;
            Mock <IBookServices> bookServiceMock = new Mock <IBookServices>();

            BookController sut = new BookController(bookServiceMock.Object);

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => sut.Create(viewModelNull));
        }
Esempio n. 10
0
        public void Create()
        {
            var mock           = new Mock <ILogger <BookController> >();
            var dbcontext      = new ApplicationDbContextFactory().CreateDbContext(new[] { "memory" });
            var bookController = new BookController(mock.Object, dbcontext);

            Assert.NotNull(bookController);
            var res = bookController.Create();

            Assert.NotNull(res);
            Assert.IsType <ViewResult>(res);
            bookController.Dispose();
        }
        public void CreateTest_should_invoke_IBookService_Add_one_time()
        {
            var bookController = new BookController(bookServiceStub);

            var book = new BookViewMoel()
            {
                ISBN = "9789869094481", Name = "玩出好創意"
            };
            var result = bookController.Create(book);

            bookServiceStub.Received().Add(Arg.Is <BookViewMoel>
                                               (x => x.ISBN == book.ISBN && x.Name == book.Name));
        }
Esempio n. 12
0
        public void CreateGet()
        {
            // Arrange
            BookController controller = new BookController(
                booksServiceMock.Object,
                pagesServiceMock.Object,
                pageConnectionsServiceMock.Object,
                usersServiceMock.Object
                );

            // Act
            ViewResult result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
        private void BtInsert_Click(object sender, RoutedEventArgs e)
        {
            Book book = new Book()
            {
                BookID   = TransformStringIntoInt(TbBookId.Text),
                BookName = TbBookName.Text,
                ISBN     = TbISBN.Text,
            };


            Book createdBook = booksController.Create(book);

            TbBookId.Text = createdBook.BookID.ToString();
            MessageBox.Show("The book with " + createdBook.ToString() + " has been created successfully.");
            LoadBooks();
        }
Esempio n. 14
0
        public void TestCreateFail()
        {
            // Arrange
            BookController controller = new BookController();

            // Act
            BookTbl newbook = new BookTbl
            {
                Id                 = 0,
                Title              = "Test Book",
                Author             = "Test Author",
                Tag                = "Test Tag",
                Year_of_publishing = -50
            };
            var result = controller.Create(newbook) as ViewResult;

            // Assert
            Assert.AreEqual("Create", result.ViewName);
        }
Esempio n. 15
0
        public void TestCreateRedirect()
        {
            // Arrange
            BookController controller = new BookController();

            // Act
            BookTbl newbook = new BookTbl
            {
                Id                 = 0,
                Title              = "Test Book-" + Membership.GeneratePassword(8, 0),
                Author             = "Test Author-" + Membership.GeneratePassword(8, 0),
                Tag                = "Test Tag-" + Membership.GeneratePassword(4, 0),
                Image              = "https://d1e4pidl3fu268.cloudfront.net/66963e4a-ccba-4fdd-ba18-d5862fb4dba7/test.png?" + Membership.GeneratePassword(8, 0),
                Year_of_publishing = 2000
            };
            var result = controller.Create(newbook) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual("Index", result.RouteValues["action"].ToString());
        }
Esempio n. 16
0
        public void Create_ReturnsOkObjectResult_WithABook()
        {
            var createBook = ACreateBookDto();
            var book       = new Book {
                Title = createBook.Title
            };

            var mockRepo = new Mock <IBookServices>();

            mockRepo.Setup(repo => repo.Create(createBook))
            .Returns(book)
            .Verifiable();

            var controller = new BookController(mockRepo.Object);
            var result     = controller.Create(createBook);

            var okObjectResult = Assert.IsType <OkObjectResult>(result);
            var returnBook     = Assert.IsType <Book>(okObjectResult.Value);

            mockRepo.Verify();

            Assert.Equal(200, okObjectResult.StatusCode);
            Assert.Equal(createBook.Title, returnBook.Title);
        }
Esempio n. 17
0
        private static void ConfigRouter()
        {
            IDataAccess      context    = Config.Instance.IDataAccess;
            BookController   controller = new BookController(context);
            ShellController  shell      = new ShellController(context);
            ConfigController config     = new ConfigController();

            Router.Instance.Register("about", About);
            Router.Instance.Register("help", Help);
            Router.Instance.Register
            (
                route: "create",
                action: p => controller.Create(),
                help: "[create] : bắt đầu nhập sách mới"
            );
            Router.Instance.Register
            (
                route: "do create",
                action: p => controller.Create(ToBook(p)),
                help: "[do create] : this route should only be used code"
            );
            Router.Instance.Register
            (
                route: "update",
                action: p => controller.Update(Int32.Parse(p["id"])),
                help: "[update ? id = <value>] : tìm và cập nhật sách theo id"
            );
            Router.Instance.Register
            (
                route: "do update",
                action: p => controller.Update(Int32.Parse(p["id"]), ToBook(p)),
                help: "[do update] : this route should only be used code"
            );
            Router.Instance.Register
            (
                route: "list",
                action: p => controller.List(),
                help: "[list] : hiển thị tất cả sách"
            );
            Router.Instance.Register
            (
                route: "single",
                action: p => controller.Single(Int32.Parse(p["id"])),
                help: "[single ? id = <value>] : hiển thị thông tin 1 cuốn sách theo id"
            );
            Router.Instance.Register
            (
                route: "single file",
                action: p => controller.Single(Int32.Parse(p["id"]), p["path"]),
                help: "[single file ? id = <value> & path = <value>] : hiển thị thông tin 1 cuốn sách theo id, xuất thông tin ra file theo đường dẫn path"
            );
            Router.Instance.Register
            (
                route: "list file",
                action: p => controller.List(p["path"]),
                help: "[list file ? path = <value>] : xuất thông tin các cuốn sách ra file theo đường dẫn path"
            );
            Router.Instance.Register
            (
                route: "delete",
                action: p => controller.Delete(Int32.Parse(p["id"])),
                help: "[delete ? id = <value>] : xóa sách theo id"
            );
            Router.Instance.Register
            (
                route: "do delete",
                action: p => controller.Delete(Int32.Parse(p["id"]), true),
                help: "[do delete] : this route should ony be used in code"
            );
            Router.Instance.Register
            (
                route: "filter",
                action: p => controller.Filter(p["key"]),
                help: "[filter ? key = <value>] : lọc sách theo key"
            );
            Router.Instance.Register
            (
                route: "add shell",
                action: p => shell.Shell(p["path"], p["format"]),
                help: "[add shell ? path = <value>] : thêm các sách trong thư mục theo đường dẫn"
            );
            Router.Instance.Register
            (
                route: "read",
                action: p => shell.Read(Int32.Parse(p["id"])),
                help: "[read ? id = <value>] : đọc sách theo id"
            );
            Router.Instance.Register
            (
                route: "mark",
                action: p => controller.Mark(Int32.Parse(p["id"])),
                help: "[mark ? id = <value>] : đánh dấu sách đang đọc theo id"
            );
            Router.Instance.Register
            (
                route: "unmark",
                action: p => controller.Mark(Int32.Parse(p["id"]), false),
                help: "[unmark ? id = <value>] : bỏ đánh dấu sách đang đọc theo id"
            );
            Router.Instance.Register
            (
                route: "show mark",
                action: p => controller.ShowMarks(),
                help: "[show mark] : hiển thị danh sách các cuốn sách đang đọc"
            );
            Router.Instance.Register
            (
                route: "clear",
                action: p => controller.Clear(),
                help: "[clear] : xóa toàn bộ sách"
            );
            Router.Instance.Register
            (
                route: "do clear",
                action: p => controller.Clear(true),
                help: "[do clear] : this route should only be used in code"
            );
            Router.Instance.Register
            (
                route: "save",
                action: p => shell.Save(),
                help: "[save] : lưu thay đổi"
            );
            Router.Instance.Register
            (
                route: "show stats by folder",
                action: p => controller.StatsByFolder(),
                help: "[show stats by folder] : thống kê sách theo thư mục"
            );
            Router.Instance.Register
            (
                route: "config promt text",
                action: p => config.ConfigPromtText(p["text"]),
                help: "[config promt text ? text = <value>]"
            );
            Router.Instance.Register
            (
                route: "config promt color",
                action: p => config.ConfigPromtColor(p["color"]),
                help: "[config promt color ? color = <value>]"
            );
            Router.Instance.Register
            (
                route: "current data access",
                action: p => config.CurrentDataAccess(),
                help: "[current data access]"
            );
            Router.Instance.Register
            (
                route: "config data access",
                action: p => config.ConfigDataAccess(p["da"], p["file"]),
                help: "[config data access ? da = <value: json, xml, binary> & file = <value>]"
            );

            #region Helper
            Models.Book ToBook(Parameter p)
            {
                Models.Book b = new Models.Book();
                if (p.IsContain("id"))
                {
                    b.Id = Int32.Parse(p["id"]);
                }
                if (p.IsContain("title"))
                {
                    b.Title = p["title"];
                }
                if (p.IsContain("authors"))
                {
                    b.Authors = p["authors"];
                }
                if (p.IsContain("publisher"))
                {
                    b.Publisher = p["publisher"];
                }
                if (p.IsContain("year"))
                {
                    b.Year = Int32.Parse(p["year"]);
                }
                if (p.IsContain("edition"))
                {
                    b.Edition = Int32.Parse(p["edition"]);
                }
                if (p.IsContain("isbn"))
                {
                    b.Isbn = p["isbn"];
                }
                if (p.IsContain("tags"))
                {
                    b.Tags = p["tags"];
                }
                if (p.IsContain("description"))
                {
                    b.Description = p["description"];
                }
                if (p.IsContain("rate"))
                {
                    b.Rating = Int32.Parse(p["rate"]);
                }
                if (p.IsContain("reading"))
                {
                    b.Reading = p["reading"].ToBool();
                }
                if (p.IsContain("file"))
                {
                    b.File = p["file"];
                }
                return(b);
            }

            #endregion
        }