如何从Amazon MWS求出单位测试



上下文

我正在单元测试C#.NET类,该类调用Amazon MWS API中的ListMatchingProducts操作(使用MWSClientCsruntime(。

essue

Amazon MWS API是一个移动的目标,其产品数据一直都在更改,因此我希望能够iaq iap返回的 ListMatchingProductsResponse对象。我可以使用MWS scratchpad提出API响应,然后将其存储在XML文件中。但是,在单元测试中,我需要将这些文件的数据胁到ListMatchingProductsResponse对象。

问题

如何将此XML数据加载到ListMatchingProductsResponse对象中?(我注意到该对象具有ReadFragmentsFrom方法,但我看不出如何使用(。

代码

[TestClass]
public class PossibleAmazonProductMatchesTests
{
    string testDataDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"Test data";
    [TestMethod]
    public void FindSpanners()
    {
        // Arrange
        ListMatchingProductsRequest request = new ListMatchingProductsRequest("secret key", "market id", "spanner");
        ListMatchingProductsResult result = new ListMatchingProductsResult();
        ListMatchingProductsResponse response = new ListMatchingProductsResponse();
        string xmlString = File.ReadAllText(this.testDataDirectory + @"Spanners Response.xml");
        // *** The issue - How do I coerce xmlString into response? ***
        var client = new Mock<MarketplaceWebServiceProductsClient>();
        client.Setup(c => c.ListMatchingProducts(request)).Returns(response);
        // Act
        // This is the method being tested. It calls ListMatchingProducts which is being mocked.
        PossibleAmazonProductMatches possibleAmazonProductMatches = new PossibleAmazonProductMatches("spanners", client);
        // Assert
        Assert.IsTrue(possibleAmazonProductMatches.SpannersFound == true);
    }
}

这看起来像是必须读取XML文件然后从XML到所需对象类型的简单情况。

更好,但是您可以在执行您所需行为的服务背后抽象,而无需紧密耦合代码与实施问题。

将MWS视为第三部分服务,并将其包裹在您完全控制的抽象背后。这样,您可以在测试时配置所需的行为。

基于@nkosi和@scottg的出色响应,我现在有一个工作解决方案,事实证明这是一个难以辨认的简单,尽管有几个要注意的要点。因此,单位测试代码进行:

[TestClass]
public class PossibleAmazonProductMatchesTests
{
    [TestMethod]
    public void Test1()
    {
        // Arrange
        var moqClient = new MarketplaceWebServiceProductsMock();
        // Act
        PossibleAmazonProductMatches possibleAmazonProductMatches = new PossibleAmazonProductMatches("spanners", moqClient);
        // Assert
        Assert.IsTrue(possibleAmazonProductMatches.PossibleProductList.Count == 10);
    }
}

..就是这样。它可以得到任何简单的!

用于抽象的测试对象(possilepleamazonproductMatches(具有此构造函数:

public PossibleAmazonProductMatches(string searchTerm, MarketplaceWebServiceProducts.MarketplaceWebServiceProducts client)
{
    // Some processing
}

要注意的要点是:

  • MarketPlaceWebServiceProducts实际上是接口,尽管它没有遵循Isomething命名约定。
  • MarketPlaceWebServiceProducts也用作命名空间名称,因此需要在Possilebleamazonproductuctuctes constructor中添加加倍的MarketPlaceWebServiceProducts.MarketPlaceWebServiceProducts语法。
  • MWS ProfoductSmock包含在MWS软件包中,因此无需代码。
  • 默认情况下,从埋葬在汇编中的固定XML模板文件中读取并使用它来构建您的测试响应。您根据需要编辑此文件。实际上,我想创建自己的XML文件,这些文件来自MWS ScratchPad,并希望将其存储在更方便的位置。我以为我可以从MarketPlaceWebServiceProductsmock继承并覆盖相关的代码来执行此操作,但事实证明这是用私人方法埋葬的。因此,我只是简单地复制了MarketPlaceWebServiceProductsmock并将其更改以满足我的需求。因此,我的模拟现在看起来像这样:

    using MarketplaceWebServiceProducts.Model;
    using System;
    using System.IO;
    using MWSClientCsRuntime;
    namespace AmazonMWS.Tests
    {
    public class MyMWSMock : MarketplaceWebServiceProducts.MarketplaceWebServiceProducts
    {
        // Definitions of most methods removed for brevity. They all match the pattern of ListMatchingProductsResponse.
        public ListMatchingProductsResponse ListMatchingProducts(ListMatchingProductsRequest request)
        {
            return newResponse<ListMatchingProductsResponse>();
        }
        private T newResponse<T>() where T : IMWSResponse
        {
            FileStream xmlIn = File.Open("D:\MyTestDataFolder\Test1.xml", FileMode.Open);
            try
            {
                StreamReader xmlInReader = new StreamReader(xmlIn);
                string xmlStr = xmlInReader.ReadToEnd();
                MwsXmlReader reader = new MwsXmlReader(xmlStr);
                T obj = (T)Activator.CreateInstance(typeof(T));
                obj.ReadFragmentFrom(reader);
                obj.ResponseHeaderMetadata = new ResponseHeaderMetadata("mockRequestId", "A,B,C", "mockTimestamp", 0d, 0d, new DateTime());
                return obj;
            }
            catch (Exception e)
            {
                throw MwsUtil.Wrap(e);
            }
            finally
            {
                if (xmlIn != null) { xmlIn.Close(); }
            }
        }
    }
    

    }

c#客户端库已经存在了几年,并在开箱即用。您不必处理任何XML,包括应有的序列化。如果您不想使用他们的代码,而是想看看它的完成方式,请将其打开到Visual Studio中并复制所需的零件。我使用了所有C#库,它们很好。您可以找到想要的操作,并输入他们的代码并运行。我相信他们有示例XML数据,并具有您将从各种操作中获得的所有响应。

最新更新