📜 ⬆️ ⬇️

We create the first application on the NancyFX part five. Application Testing

In previous articles, I showed you how you can easily create and configure an application on NancyFX to fit your needs. However, at the present time, it is simply not possible to imagine the process of industrial software development in isolation from testing. Let's add a project to our first application to test it.

NancyFX provides the Nancy.Testing library for testing Nancy applications, which we can add to our applications using NuGet. And so, let's add a test project to the solution with our first application. (In this example, I will use the MSTest framework for convenience, this framework can be easily replaced with nUnit or with any other test framework, without any significant labor costs ).



Now let's add a link to the test project to our test project.
')


Next, run the NuGet Package Manager and add the Nancy.Testing library to the project.



We need to add a class that implements the IRootPathProvider.r interface to the test project. Immediately, I’ll make a reservation that this is related to the error that occurs when running the NancyFX tests in conjunction with the MSTest framework. When working with the nUnit framework, adding this class is not required. And so we add TestRootPathProvider the only method of which should return the absolute path the path to the tested project. The provider's code should look like this:

using Nancy; namespace NancyTestProject { public class TestRootPathProvider : IRootPathProvider { public string GetRootPath() { return @"C:\......\GitHub\NancyFxApplication\NancyFxApplication"; } } } 


Now we add a new test to the project. In the parameters of the bootstrapper which we must indicate which module we will be testing, and also indicate the provider of the path to the project under test, which we defined earlier. Later in the test code, we create an instance of the Browser class with which we can invoke our application. This is followed by a checkout block, where we will use asserts to check the correctness of the operation of our module.

 using Microsoft.VisualStudio.TestTools.UnitTesting; using Nancy; using Nancy.Testing; using NancyFxApplication; namespace NancyTestProject { [TestClass] public class NancyTest { [TestMethod] public void simplest_get_test() { var bootstrapper = new ConfigurableBootstrapper(with => { with.Module<NancyFxModule>(); with.RootPathProvider<TestRootPathProvider>(); }); // Given var browser = new Browser(bootstrapper); // When var result = browser.Get("/", with => { with.HttpRequest(); }); // Then Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); } } } 


Now we run our test in the explorer test and get the following result:



As you can see, setting up the test environment took just a few minutes. Now you can easily add new tests while testing your application. In conclusion, I would like to make a brief announcement of the last article of the cycle. She will be dedicated to Nancy.SelfHosting.
Thank you for your attention, waiting for your feedback and comments.

Source: https://habr.com/ru/post/203346/


All Articles