- Instant Nancy Web Development
- Christian Horsdal
- 424字
- 2021-08-04 09:59:26
How to do it...
The following steps will help you write tests for your Nancy application:
- Open Visual Studio.
- Create a new empty C# ASP.NET project and call it
TodoNancy
. - Create a new C# Class Library project and call it
TodoNancyTests
. - Go to Package Manager Console and write the following line of code in it:
PM> Install-Package Nancy.Testing TodoNancyTests
- This pulls down the
Nancy.Testing NuGet
package and installs it into theTodoNancyTests
project. This package contains Nancy's support for testing. - Go back to the Package Manager Console and write the following line of code to install the xUnit test framework:
PM> Install-Package xunit TodoNancyTests
- Add a project reference to the
TodoNancy
project in theTodoNancyTests
project. This is done by right-clicking on References under the TodoNancyTests project, choosing Add Reference..., and in the dialog that opens, find the TodoNancy project under Solution | Projects. Your solution should now look like the following screenshot: - There is a file called Class1.cs in the TodoNancyTests project. Rename it to
HomeModuleTests
and put the following code in it:namespace TodoNancyTests { using Nancy; using Nancy.Testing; using Xunit; public class HomeModuleTests { [Fact] public void Should_answer_200_on_root_path() { var sut = new Browser(new DefaultNancyBootstrapper()); var actual = sut.Get("/"); Assert.Equal(HttpStatusCode.OK, actual.StatusCode); } } }
- This test simulates HTTP
GET "/"
to our Nancy application. We have not added any modules to our application, so it should not pass. Run the test and watch it fail. The test can be run using the xUnit.net test runner for Visual Studio (http://visualstudiogallery.msdn.microsoft.com/463c5987-f82b-46c8-a97e-b1cde42b9099) or using TestDriven.NET (http://testdriven.net/). - Note the variable naming in the test. They are a part of another set of conventions I like to follow. The
sut
variable is the thing being tested. It is short for system under test. Theactual
variable is the result of what the test does to the production code. Consequently,actual
usually holds the value asserted at the end of the test. - Add the Nancy framework to the
TodoNancy
project, and enable running on top of ASP.NET by going to Package Manager Console and typing in this command:PM> Install-Package Nancy.Hosting.Aspnet TodoNancy
- To make the test pass, we need to add a new C# file to the root of the
TodoNancy
project. Let's call itHelloModule
and add the following code to it:namespace TodoNancy { using Nancy; public class HomeModule : NancyModule { public HomeModule() { Get["/"] = _ => HttpStatusCode.OK; } } }
- Your Solution Explorer should now look more or less as the following screenshot:
- Re-run the test from beginning and watch it succeed.
推薦閱讀
- Puppet 4 Essentials(Second Edition)
- Implementing VMware Horizon 7(Second Edition)
- Flask Web開發(fā)入門、進階與實戰(zhàn)
- Ray分布式機器學(xué)習(xí):利用Ray進行大模型的數(shù)據(jù)處理、訓(xùn)練、推理和部署
- 機械工程師Python編程:入門、實戰(zhàn)與進階
- 假如C語言是我發(fā)明的:講給孩子聽的大師編程課
- 零基礎(chǔ)輕松學(xué)SQL Server 2016
- Swift細致入門與最佳實踐
- Learning Raspbian
- ANSYS Fluent 二次開發(fā)指南
- Visual Basic程序設(shè)計
- HTML+CSS+JavaScript編程入門指南(全2冊)
- Instant Debian:Build a Web Server
- 貫通Tomcat開發(fā)
- Go Systems Programming