title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
21:2028 | At this point, we are ready to write our tests. |
21:2029 | As an example, we will write unit tests for the Edit method decorated with [HttpPost] of the ManagePackagesController controller, which is shown as follows: |
21:2030 | [HttpPost] |
21:2031 | public async Task<IActionResult> Edit( |
21:2032 | PackageFullEditViewModel vm, |
21:2033 | [FromServices] ICommandHandler<UpdatePackageCommand> command) |
21:2034 | { |
21:2035 | if (ModelState.IsValid) |
21:2036 | { |
21:2037 | await command.HandleAsync( |
21:2039 | return RedirectToAction( |
21:2040 | nameof(ManagePackagesController.Index)); |
21:2041 | } |
21:2042 | else |
21:2043 | return View(vm); |
21:2044 | } |
21:2045 | |
21:2046 | Before writing our test methods, let’s rename the test class that was automatically included in the test project as ManagePackagesControllerTests. |
21:2047 | The first test verifies that if there are errors in ModelState, the action method renders a view with the same model it received as an argument so that the user can correct all errors. We need to test all possibilities. Let’s delete the existing test method and write an empty DeletePostValidationFailedTest method, as follows: |
21:2048 | [Fact] |
21:2049 | public async Task DeletePostValidationFailedTest() |
21:2050 | { |
21:2051 | } |
21:2052 | |
21:2053 | The method must be async and the return type must be Task since the Edit method that we have to test is async. In this test, we don’t need mocked objects since no injected object will be used. Thus, as a preparation for the test, we just need to create a controller instance, and we must add an error to ModelState as follows: |
21:2054 | var controller = new ManagePackagesController(); |
21:2055 | controller.ModelState |
21:2056 | .AddModelError(`Name`, `fake error`); |
21:2057 | |
21:2058 | Then, we invoke the method, injecting ViewModel and a null command handler as its arguments, since the command handler will not be used: |
21:2059 | var vm = new PackageFullEditViewModel(); |
21:2060 | var commandDependency = |
21:2061 | new Mock<ICommandHandler<UpdatePackageCommand>>(); |
21:2062 | var result = await controller.Edit(vm, commandDependency.Object); |
21:2063 | |
21:2064 | In the verification stage, we verify that the result is ViewResult and that it contains the same model that was injected into the controller: |
21:2065 | var viewResult = Assert.IsType<ViewResult>(result); |
21:2066 | Assert.Equal(vm, viewResult.Model); |
21:2067 | |
21:2068 | Now, we also need a test to verify that if there are no errors, the command handler is called, and then the browser is redirected to the Index controller action method. We call the DeletePostSuccessTest method: |
21:2069 | [Fact] |
21:2070 | public async Task DeletePostSuccessTest() |
21:2071 | { |
21:2072 | } |
21:2073 | |
21:2074 | This time, the preparation code must include the preparation of a command handler mock, as follows: |
21:2075 | var controller = new ManagePackagesController(); |
21:2076 | var commandDependency = |
21:2077 | new Mock<ICommandHandler<UpdatePackageCommand>>(); |
21:2078 | commandDependency |
21:2079 | .Setup(m => |
21:2081 | .Returns(Task.CompletedTask); |
21:2082 | var vm = new PackageFullEditViewModel(); |
21:2083 | |
21:2084 | Since the handler HandleAsync method returns no async value, we can’t use ReturnsAsync, but we have to return just a completed Task (Task.Complete) with the Returns method. The method to test is called with both ViewModel and the mocked handler: |
21:2085 | var result = await controller.Edit(vm, |
21:2086 | commandDependency.Object); |
21:2087 | |
21:2088 | In this case, the verification code is as follows: |
21:2089 | commandDependency.Verify(m => m.HandleAsync( |
21:2090 | It.IsAny<UpdatePackageCommand>()), |
21:2091 | Times.Once); |
21:2092 | var redirectResult = Assert.IsType<RedirectToActionResult>(result); |
21:2093 | Assert.Equal(nameof(ManagePackagesController.Index), |
21:2094 | redirectResult.ActionName); |
21:2095 | Assert.Null(redirectResult.ControllerName); |
21:2096 | |
21:2097 | As the first step, we verify that the command handler has actually been invoked once. A better verification should also include a check that it was invoked with a command that includes ViewModel passed to the action method. We will take it up as an exercise. |
21:2098 | Then we verify that the action method returns RedirectToActionResult with the right action method name and with no controller name specified. |
21:2099 | Once all the tests are ready, if the test window does not appear on the left bar of Visual Studio, we may simply select the Run all tests item from the Visual Studio Test menu. Once the test window appears, further invocations can be launched from within this window. |
21:2100 | If a test fails, we can add a breakpoint to its code, so we can launch a debug session on it by right-clicking on it in the test window and then selecting Debug selected tests. It is worth remembering that failures do not depend necessarily on errors in the code under test but might also depend on errors in the testing code itself. |
21:2101 | In the next subsection, we will show how to upload our code to a shared Azure DevOps repository and how to automatize our tests with an Azure DevOps pipeline. |
21:2102 | Connecting to an Azure DevOps repository |
21:2103 | Tests play a fundamental role in the application CI/CD cycle, specifically in CI. They must be executed at least each time the master branch of the application repository is modified to verify that changes don’t introduce bugs. |
21:2104 | The following steps show how to connect our solution to an Azure DevOps repository, where we will define an Azure DevOps pipeline that builds the project, and that also launches all the unit tests we defined in the PackagesManagementTest project at each build. |
21:2105 | However, the functional tests that we defined in the PackagesManagementFTest project must be executed only before a sprint is released. Therefore, they must be placed in a different pipeline that takes care of delivering the application. |
21:2106 | In this way, every day after all developers have pushed their changes, we can launch the pipeline to verify that the repository code compiles and passes all the unit tests: |
21:2107 | |
21:2108 | As a first step, we need a free DevOps subscription. If you don’t already have one, please create one by clicking the Start free button on this page: https://azure.microsoft.com/en-us/services/devops/. Here, let’s follow the wizard to define an organization and then a project. |
21:2109 | On the project page, select the Files menu, click on the Repos menu item, and then copy the repository URL: |
21:2110 | |
21:2111 | |
21:2112 | Figure 21.30: Copying the repository URL |
21:2113 | |
21:2114 | Ensure you are logged in to Visual Studio with your Azure account (the same one used in the creation of the DevOps account). In the Git Changes tab, click the Create Git Repository... button: |
21:2115 | |
21:2116 | |
21:2117 | Figure 21.31: Opening the connection window |
21:2118 | |
21:2119 | In the window that opens, select Existing remote from the left menu and copy and paste the remote repository URL: |
21:2120 | |
21:2121 | |
21:2122 | Figure 21.32: Connection window |
21:2123 | |
21:2124 | Then, click the Create and Push button and wait until the ready icon in the bottom-left corner of Visual Studio is checked: |
21:2125 | |
21:2126 | |
21:2127 | Figure 21.33: Operation completed |
21:2128 | |
21:2129 | At this point, the repository has been created locally, connected with the selected remote repository, and all changes have been committed and pushed to the remote repository. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.