mercredi 11 novembre 2015

need help in untit testing in asp.net mvc

i am asked to do tests for a Controller in asp.net mvc architecture . i don't have a clue how to do it .it would be very kind if someone could give me a push by creating the first Testmethod so i can follow the example. Heres the code of the first methode in the Controller Class :

namespace BookShop.MVC.Controllers
{
    [Authorize]
    public class BooksController : Controller
    {
        private readonly IBookManager _bookManager;
        `//constructor 
        public BooksController(IBookManager bookManager)
        {
            _bookManager = bookManager;
        }

        // GET: Books/Archive
        public async Task<ActionResult> Archive()
        {
            var usersBooks = await _bookManager.GetArchiveBooks(Guid.Parse(HttpContext.User.Identity.GetUserId()));

            var result = usersBooks.Select(ConvertArchiveBookToViewModel).ToList();

            return View(new ArchiveViewModel
            {
                Books = result
            });
        }

and here is the code of the IBookManager interface (i am not sure if you need it but i saw in internet something about mocking interfaces )

namespace BookShop.Services
{
    public interface IBookManager
    {
        /// <summary>
        /// Gibt das private Archiv eines Benutzers zurück.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task<List<ArchiveBook>> GetArchiveBooks(Guid userId);

        /// <summary>
        /// Gibt alle Bücher des Schaufensters zurück.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task<List<ShowRoomBook>> GetShowRoomBooks(Guid userId);

        /// <summary>
        /// Gibt ein einzelnes Buch aus dem privaten Archiv zurück.
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task<ArchiveBook> GetArchiveBook(Guid bookId, Guid userId);

        /// <summary>
        /// Gibt ein einzelnes Buch aus dem Schaufenster zurück.
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task<ShowRoomBook> GetShowRoomBook(Guid bookId, Guid userId);

        /// <summary>
        /// Ändert den Besitzer eines Buches.
        /// </summary>
        /// <param name="bookId">Die ID des Buches, dessen Besitzer geändert werden soll.</param>
        /// <param name="userId">Die ID des neuen Besitzers.</param>
        /// <returns></returns>
        Task ChangeBookOwner(Guid bookId, Guid userId);

        /// <summary>
        /// Erstellt ein neues Buch.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <param name="yearOfPublication"></param>
        /// <param name="isbn"></param>
        /// <param name="image"></param>
        /// <param name="description"></param>
        /// <param name="author"></param>
        /// <returns></returns>
        Task<ArchiveBook> CreateBook(Guid userId, string title, string author, short yearOfPublication, string isbn,
            byte[] image, string description);

        /// <summary>
        /// Löscht ein Buch.
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task DeleteBook(Guid bookId, Guid userId);

        /// <summary>
        /// Setzt die Verkaufsinformationen eines Buches.
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <param name="price"></param>
        /// <param name="shippingCosts"></param>
        /// <param name="packingCosts"></param>
        /// <returns></returns>
        Task<ArchiveBook> SetBookPurchaseInformation(Guid bookId, Guid userId, double price, double shippingCosts,
            double packingCosts);

        /// <summary>
        /// Löscht die Verkaufsinformationen eines Buches.
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        Task<ArchiveBook> ClearBookPurchaseInformation(Guid bookId, Guid userId);
    }
}

I really apreciate your help :)

Aucun commentaire:

Enregistrer un commentaire