Do I need different module in each test case using Nancy?

时间:2016-10-20 13:13:18

标签: selenium testing automated-tests nancy

I want to test my web application, without the web services layer. in order to do that I am using Nancy framework.

I am mocking ServiceA as follow:

public class ServiceAModule : NancyModule
{
    public ServiceAModule () : base("/serviceAPath")
    {
        Get["/"] = p =>
        {
            var s = @"{Property1 : 23}";
            var jsonBytes = Encoding.UTF8.GetBytes(s);

            return new Response
            {
                ContentType = "application/json",
                Contents = stream => stream.Write(jsonBytes, 0, jsonBytes.Length),
                StatusCode = HttpStatusCode.OK
            };
        };
    }

Now, in my tests: I initialize Nancy service:

    private static IDisposable CreateService()
    {
        const string url = "http://+:8088";

        var service = WebApp.Start(url, builder =>
        {
            var browser = new Browser(with => { with.EnableAutoRegistration(); });

            builder.UseNancy();
        });

        return service;
    }

And I am testing the application UI using selenium. My question is: I need different scenario (different response from ServiceAModule Get end point), what are my options? As I see it, I have one option, which is to create different module for each test case and register this module on each test. This solution brings a lot of code, and mess.

Do I have any other option? what is the common use of Nancy in this cases?

Thank you!

1 个答案:

答案 0 :(得分:0)

不同的回答是什么意思?您可以在同一模块

上添加任意数量的操作
public ServiceAModule () : base("/serviceAPath")
{
    Get["/"] = p => Response.AsJson(new {Property1 : 23 });
    Post["/"] = p => Response.AsText("Saved !!!");
    Get["/thing"] = p => Response.AsJson(new { foo : 3434 , bar : 900 });

}