- Instant Nancy Web Development
- Christian Horsdal
- 323字
- 2021-08-04 09:59:26
How it works...
Congratulations! You just created your first piece of the Nancy application code in a test-first fashion.
Taking a closer look at the test code, the first thing to notice is the following line of code:
var sut = new Browser(new DefaultNancyBootstrapper());
This creates an instance of the Browser
class from the Nancy.Testing
namespace, which is an essential type of object while testing Nancy modules. The Browser
type allows making calls that simulate real HTTP requests without neither the Nancy framework, nor your application code knowing the difference. Furthermore, these simulated requests are made without actually going through the network stack. This is important because it gives you the opportunity to write tests that are both fast and run against the API you expose to clients; for example, browsers.
The next thing to note is the following line of code:
var actual = sut.Get("/");
This line uses the Get
method of the Browser
object, which will create what appears to the Nancy framework as an HTTP GET request to the path provided as the first argument—in this case "/"
. This is done in-process and does not involve the network stack, which means that it is a lot faster and easier to set up than tests that make full-fledged real HTTP requests. Also worth noting is how this testing syntax aligns nicely with the syntax used in the Nancy module to set up the handler for the route.
The return value from the call to Get
is a BrowserResponse
class—another class from the Nancy.Testing
namespace. The BrowserResponse
class gives the tests the access to everything returned from the route handler as well as everything Nancy added to that response. The different parts of the response can be reached through the properties such as StatusCode
, Headers
, Cookies
, and Body
. In the test we wrote in the previous section, we just read StatusCode
and asserted that it was 200 OK
.
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- Learn Type:Driven Development
- Python進階編程:編寫更高效、優雅的Python代碼
- INSTANT Sencha Touch
- PHP 編程從入門到實踐
- Mastering Unity Shaders and Effects
- 快速念咒:MySQL入門指南與進階實戰
- Python算法從菜鳥到達人
- Visual Basic程序設計實驗指導(第二版)
- 精通Python自動化編程
- 區塊鏈技術與應用
- Azure Serverless Computing Cookbook
- Java并發編程之美
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- LabVIEW入門與實戰開發100例(第4版)