Guest User

Untitled

a guest
Nov 21st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class GetByIdTests : BaseTest
  2. {
  3. protected Mock<IProductRepository> Repository { get; private set; }
  4.  
  5. protected ProductLogic Create()
  6. {
  7. Repository = new Mock<IProductRepository>();
  8.  
  9. return new ProductLogic(new Lazy<IProductRepository>(() => Repository.Object));
  10. }
  11.  
  12. [Fact]
  13. public void Return_Product_From_Repository()
  14. {
  15. var logic = Create();
  16.  
  17. var product = Builder<Product>.CreateNew().Build();
  18.  
  19. Repository.Setup(r => r.GetById(It.IsAny<int>()))
  20. .Returns(product);
  21.  
  22. var result = logic.GetById(10);
  23.  
  24. Assert.NotNull(result);
  25. Assert.True(result.Success);
  26. Assert.Equal(product, result.Value);
  27. Assert.NotNull(result.Errors);
  28. Assert.Equal(0, result.Errors.Count());
  29.  
  30. Repository.Verify(r => r.GetById(10), Times.Once());
  31. }
  32.  
  33. [Fact]
  34. public void Return_Error_When_Product_Not_Exist()
  35. {
  36. var logic = Create();
  37.  
  38. Repository.Setup(r => r.GetById(It.IsAny<int>()))
  39. .Returns((Product)null);
  40.  
  41. var result = logic.GetById(10);
  42.  
  43. Assert.NotNull(result);
  44. Assert.False(result.Success);
  45. Assert.Null(result.Value);
  46. Assert.NotNull(result.Errors);
  47. Assert.Equal(1, result.Errors.Count());
  48.  
  49. var error = result.Errors.First();
  50.  
  51. Assert.Equal(string.Empty, error.PropertyName);
  52. Assert.Equal("Nie ma produktu o id 10.", error.Message);
  53.  
  54. Repository.Verify(r => r.GetById(10), Times.Once());
  55. }
  56. }
Add Comment
Please, Sign In to add comment