Created
December 4, 2018 05:27
FluentAssertions.Mvc - assercie dla ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseTest | |
{ | |
protected Mock<IProductLogic> Logic { get; set; } | |
protected Mock<IMapper> Mapper { get; set; } | |
protected virtual ProductsController Create() | |
{ | |
Logic = new Mock<IProductLogic>(); | |
Mapper = new Mock<IMapper>(); | |
return new ProductsController(new Lazy<IProductLogic>(() => Logic.Object), | |
new Lazy<IMapper>(() => Mapper.Object)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ControllerExtensions | |
{ | |
public static ControllerAssertions Should(this Controller instance) | |
{ | |
return new ControllerAssertions(instance); | |
} | |
} | |
public class ControllerAssertions : ReferenceTypeAssertions<Controller, ControllerAssertions> | |
{ | |
public ControllerAssertions(Controller controller) | |
{ | |
Subject = controller; | |
} | |
protected override string Identifier => "controller"; | |
public ControllerAssertions HasError(string property, string message, string because = "", | |
params object[] becauseArgs) | |
{ | |
var state = Subject.ModelState.FirstOrDefault(m => m.Key == property); | |
Execute.Assertion | |
.BecauseOf(because, becauseArgs) | |
.ForCondition(state.Key == property) | |
.FailWith($"The controller should have error for property: {property}."); | |
Execute.Assertion | |
.BecauseOf(because, becauseArgs) | |
.ForCondition(state.Value.Errors.Any(e => e.ErrorMessage == message)) | |
.FailWith($"The controller should have error with message: {message}."); | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateTests : BaseTest | |
{ | |
protected ProductViewModel ViewModel { get; set; } | |
protected Product Product { get; set; } | |
protected Result<Product> ProductResult { get; set; } | |
protected override ProductsController Create() | |
{ | |
var controller = base.Create(); | |
Product = Builder<Product>.CreateNew().Build(); | |
ProductResult = Result.Ok(Product); | |
Logic.Setup(l => l.Create(It.IsAny<Product>())) | |
.Returns(() => ProductResult); | |
ViewModel = Builder<ProductViewModel>.CreateNew().Build(); | |
Mapper.Setup(m => m.Map<Product>(It.IsAny<ProductViewModel>())) | |
.Returns(Product); | |
return controller; | |
} | |
[Fact] | |
public void Return_View_With_Errors_When_Result_Is_Failure() | |
{ | |
var controller = Create(); | |
ProductResult = Result.Failure<Product>("Property", "Error"); | |
var result = controller.Create(ViewModel); | |
result.Should() | |
.BeViewResult() | |
.WithDefaultViewName() | |
.Model | |
.Should() | |
.BeEquivalentTo(ViewModel); | |
controller.Should() | |
.HasError("Property", "Error"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DetailsTests : BaseTest | |
{ | |
protected ProductViewModel ViewModel { get; set; } | |
protected Product Product { get; set; } | |
protected Result<Product> ProductResult { get; set; } | |
protected override ProductsController Create() | |
{ | |
var controller = base.Create(); | |
Product = Builder<Product>.CreateNew().Build(); | |
ProductResult = Result.Ok(Product); | |
Logic.Setup(l => l.GetById(It.IsAny<int>())) | |
.Returns(() => ProductResult); | |
ViewModel = Builder<ProductViewModel>.CreateNew().Build(); | |
Mapper.Setup(m => m.Map<ProductViewModel>(It.IsAny<Product>())) | |
.Returns(ViewModel); | |
return controller; | |
} | |
[Fact] | |
public void Redirect_To_Index_When_Result_Is_Failure() | |
{ | |
var controller = Create(); | |
ProductResult = Result.Failure<Product>("Property", "Error"); | |
var result = controller.Details(10); | |
result.Should() | |
.BeRedirectToRouteResult() | |
.WithAction("Index"); | |
} | |
[Fact] | |
public void Return_View_With_Data_When_Result_Is_Failure() | |
{ | |
var controller = Create(); | |
var result = controller.Details(10); | |
result.Should() | |
.BeViewResult() | |
.WithDefaultViewName() | |
.Model | |
.Should() | |
.BeEquivalentTo(ViewModel); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IProductLogic | |
{ | |
Result<Product> GetById(int id); | |
Result<Product> Create(Product product); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductsController : Controller | |
{ | |
private Lazy<IProductLogic> _logic; | |
protected IProductLogic Logic | |
{ | |
get { return _logic.Value; } | |
} | |
private Lazy<IMapper> _mapper; | |
protected IMapper Mapper | |
{ | |
get { return _mapper.Value; } | |
} | |
public ProductsController(Lazy<IProductLogic> logic, | |
Lazy<IMapper> mapper) | |
{ | |
_logic = logic; | |
_mapper = mapper; | |
} | |
public ActionResult Details(int id) | |
{ | |
var result = Logic.GetById(id); | |
if (result.Success == false) | |
{ | |
return RedirectToAction("Index"); | |
} | |
var viewModel = Mapper.Map<ProductViewModel>(result.Value); | |
return View(viewModel); | |
} | |
public ActionResult Create() | |
{ | |
return View(new ProductViewModel()); | |
} | |
[HttpPost] | |
public ActionResult Create(ProductViewModel viewModel) | |
{ | |
var product = Mapper.Map<Product>(viewModel); | |
var result = Logic.Create(product); | |
if (result.Success == false) | |
{ | |
result.AddErrorToModelState(ModelState); | |
return View(viewModel); | |
} | |
return RedirectToAction("Index"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment