【问题标题】:Do I need different module in each test case using Nancy?在使用 Nancy 的每个测试用例中,我是否需要不同的模块?
【发布时间】:2017-03-02 12:24:43
【问题描述】:

我想在没有 Web 服务层的情况下测试我的 Web 应用程序。为了做到这一点,我正在使用 Nancy 框架。

我正在嘲笑 ServiceA 如下:

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
            };
        };
    }

现在,在我的测试中:我初始化 Nancy 服务:

    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;
    }

我正在使用 selenium 测试应用程序 UI。 我的问题是:我需要不同的场景(来自 ServiceAModule Get 端点的不同响应),我有什么选择? 如我所见,我有一个选择,即为每个测试用例创建不同的模块并在每个测试中注册该模块。 这个解决方案带来了大量的代码和混乱。

我还有其他选择吗?在这种情况下,Nancy 的常见用途是什么?

谢谢!

【问题讨论】:

    标签: selenium testing automated-tests nancy


    【解决方案1】:

    你所说的不同响应是什么意思?您可以在同一个模块

    上添加任意数量的操作
    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 });
    
    }
    

    【讨论】:

    • 这些是不同的动作,同一个动作我需要不同的响应。
    猜你喜欢
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多