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

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.

主站蜘蛛池模板: 邯郸县| 临桂县| 三河市| 疏勒县| 四平市| 瓦房店市| 成安县| 芦溪县| 西乡县| 郑州市| 喀喇沁旗| 福贡县| 崇礼县| 大足县| 长海县| 隆昌县| 右玉县| 阳春市| 呼玛县| 皮山县| 九台市| 固镇县| 电白县| 洛浦县| 三台县| 荣昌县| 永顺县| 驻马店市| 昔阳县| 棋牌| 宝清县| 阜阳市| 襄汾县| 阿瓦提县| 平定县| 东城区| 祁连县| 开阳县| 浦县| 江阴市| 满洲里市|