如何在Xamarin.UITest项目中使用WireMock.Net?

问题描述

我的UITest项目在原始Web服务器上运行良好,但是我想使用WireMock.net将其替换为模拟服务器。我已经在非UITest项目中成功使用了它。这是我在UI测试项目中的代码:

    [SetUp]
    public void BeforeEachTest()
    {
        app = AppInitializer.StartApp(platform);
    }
    
    [OneTimeSetUp]
    public void InitializerOnce()
    {
        _mockServer = WireMockServer.Start(new WireMockServerSettings()
        {
            Urls = new[] { "http://localhost:12345/"},ReadStaticMappings = true
        });
    
        _mockServer.Given(
        Request.Create().WithPath("*").UsingAnyMethod())
            .RespondWith(Response.Create()
        .WithStatusCode(HttpStatusCode.OK).WithBody("Sample response!"));
    }
    
    [OneTimeTearDown]
    public void DisposeOnce()
    {
        _mockServer?.Stop();
    }
    
    [Test]
    public async Task Test()
    {
        app.Tap(c => c.Marked("MyButton"));
    
        await Task.Delay(5000);
    
        //Assert
        Assert.IsTrue(true);
    }
    
    private WireMockServer _mockServer;

我的主要Android项目具有以下代码:

    <Button AutomationId="MyButton" Text="Action" Clicked="Action_OnClicked"/>

    private async void Action_OnClicked(object sender,EventArgs e)
    {
        try
        {
            var client = new HttpClient();
    
            var response = await client.GetAsync("http://10.0.2.2:12345/test");// is it a correct url???
            var result = await response.Content.ReadAsStringAsync();
    
            await UserDialogs.Instance.AlertAsync($"Status:{response.StatusCode},result:{result}");
        }
        catch (Exception exception)
        {
            await UserDialogs.Instance.AlertAsync(exception.ToString());
        }
    }

这是点击按钮的结果:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)