Get Īnd then in the test project I need to implement the helper method I've created above called GetController(). Public BlogPostSurfaceController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper) Public BlogPostSurfaceController(UmbracoContext umbracoContext) Private readonly UmbracoHelper _umbracoHelper Public class BlogPostSurfaceController : To support testing there's a need to make a minor amend to the controller itself, to create a constructor that allows the passing of an instance (mocked for testing) of the Umbraco context and helper: Var umbracoPageResult = result as UmbracoPageResult Īssert.IsNull(controller.TempData) Public void CreateComment_WithInValidComment_RedisplaysForm()Ĭ("Email", "Email is required.") Var redirectToUmbracoPageResult = result as RedirectToUmbracoPageResult Īssert.IsNotNull(redirectToUmbracoPageResult) Īssert.AreEqual(1000, ) Īssert.IsNotNull(controller.TempData) Var result = controller.CreateComment(model) Public void CreateComment_WithValidComment_RedirectsWithMessage() TempData.Add("CustomMessage", "Thanks for your comment.") Public ActionResult CreateComment(CommentViewModel model) I'll go back to the simple example I was looking to test in the previous blog post: You can also take a look through the code of the Umbraco REST API which as noted in the comments below also contains several examples. But there are no base classes that you are required to depend on, no tie to a particular unit testing framework and no longer a requirement for reflection code.Īs well as what follows, you can see some more examples of this in the Umbraco source code itself - which I used for reference. There's still a bit of set-up and mocking code required, but that's unavoidable to ensure the surface controller has access to things like the Umbraco context in it's operations. And given the recent release of this in beta, wanted to check it out.Īm pleased to report things are a lot easier now. One thing I've been following and looking forward to the release of version 7.3 of Umbraco is the discussion on improved testing support. Putting all that together probably explains that although I did have a method that worked it wasn't something I made part of my general work with Umbraco. Code marked as internal is done so for a reason, one of those being that even if it's potentially useful to provide public access to developers, the core team haven't yet committed to the implementation so reserve the right to change it, even in a minor upgrade. This worked, and only needed to be written once in a a base class to utilise it in all your tests, but was rather brittle. Again, a perfectly decent test runner but if you generally used MSTest or something else you had to make a switch.Īnd lastly in order to create various surface controller dependencies, you had to use some rather impenetrable looking reflection code to get around the fact that certain classes and methods were marked as internal rather than public. No great hardship to use that but it did mean you had to compile the source to get it and it led to some other restrictions on your tests. With the help of information posted by some others that had similarly looked into it I found there was a method that worked, but it wasn't ideal for a number of reasons.įirstly there was a need to utilise an Umbraco test helpers dll, that contained implementations of various base classes that could be used to support testing. A little while back now I spent a bit of time investigating how one might go about unit testing Umbraco surface controllers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |