官术网_书友最值得收藏!

  • 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:

  1. Open Visual Studio.
  2. Create a new empty C# ASP.NET project and call it TodoNancy.
  3. Create a new C# Class Library project and call it TodoNancyTests.
  4. Go to Package Manager Console and write the following line of code in it:
    PM> Install-Package Nancy.Testing TodoNancyTests
  5. This pulls down the Nancy.Testing NuGet package and installs it into the TodoNancyTests project. This package contains Nancy's support for testing.
  6. 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
  7. Add a project reference to the TodoNancy project in the TodoNancyTests 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:
  8. 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);
        }
      }
    }
  9. 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/).
  10. 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. The actual 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.
  11. 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
  12. To make the test pass, we need to add a new C# file to the root of the TodoNancy project. Let's call it HelloModule and add the following code to it:
    namespace TodoNancy
    {
      using Nancy;
    
    
      public class HomeModule : NancyModule
      {
        public HomeModule()
        {
          Get["/"] = _ => HttpStatusCode.OK;
        }
      }
    }
  13. Your Solution Explorer should now look more or less as the following screenshot:
  14. Re-run the test from beginning and watch it succeed.
主站蜘蛛池模板: 青铜峡市| 都匀市| 当雄县| 临西县| 蕉岭县| 邹平县| 扎赉特旗| 海南省| 高唐县| 怀柔区| 大渡口区| 株洲县| 梅河口市| 阜康市| 微山县| 开远市| 运城市| 安乡县| 芒康县| 麻城市| 瑞丽市| 永新县| 临武县| 长寿区| 资中县| 青铜峡市| 旬阳县| 宽甸| 兰州市| 沐川县| 神农架林区| 壶关县| 太保市| 麦盖提县| 分宜县| 北票市| 疏附县| 雷州市| 宿州市| 顺平县| 仲巴县|