PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/MusicStore.UnitTest/CartControllerTest.cs

https://github.com/satish860/MusicStoreAPI
C# | 63 lines | 59 code | 4 blank | 0 comment | 0 complexity | 44c84132d810aba43761b851c2e1877a MD5 | raw file
  1. using MusicStore.Api;
  2. using NUnit.Framework;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using FluentAssertions;
  9. using System.Web.Http;
  10. using System.Net.Http;
  11. using System.Web.Http.Routing;
  12. using System.Web.Http.Controllers;
  13. using System.Web.Http.Hosting;
  14. using System.Net;
  15. namespace MusicStore.UnitTest
  16. {
  17. [TestFixture]
  18. public class CartControllerTest
  19. {
  20. [Test]
  21. public void Should_be_Send_the_Command_to_the_Command_bus()
  22. {
  23. dynamic Controller = ConfigureController(TestInstance.CartController);
  24. CreateCartCommand command = new CreateCartCommand { Id = 1 };
  25. Controller.Post(command);
  26. TestInstance.InMemoryCommandBus.Command.Should().Be(command);
  27. }
  28. [Test]
  29. public void Should_be_able_to_Send_back_Status_Code_As_201()
  30. {
  31. dynamic Controller = ConfigureController(TestInstance.CartController);
  32. CreateCartCommand command = new CreateCartCommand { Id = 1 };
  33. HttpResponseMessage message = Controller.Post(command);
  34. message.StatusCode.Should().Be(HttpStatusCode.Created);
  35. message.Headers.Location.ToString().Should().Be("http://localhost/api/Cart/1");
  36. }
  37. [Test]
  38. public void Should_be_Send_the_Put_Command_to_the_Command_Bus()
  39. {
  40. dynamic Controller = ConfigureController(TestInstance.CartController);
  41. AddProductToCartCommand command = new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 2 };
  42. HttpResponseMessage message = Controller.PutCartCommand(command);
  43. TestInstance.InMemoryCommandBus.Command.Should().Be(command);
  44. message.StatusCode.Should().Be(HttpStatusCode.OK);
  45. }
  46. public ApiController ConfigureController(ApiController controller)
  47. {
  48. var config = new HttpConfiguration();
  49. var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
  50. var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
  51. var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
  52. controller.ControllerContext = new HttpControllerContext(config, routeData, request);
  53. controller.Request = request;
  54. controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
  55. controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
  56. return controller;
  57. }
  58. }
  59. }