如何对工厂进行单元测试?

发布于 2024-10-16 12:45:41 字数 764 浏览 5 评论 0原文

我通过为所有类提供一个接口来对我的类进行单元测试。这些接口又具有自己的模拟。

但假设我有以下内容:

class IData
{
  GetData()
}

class IOnScreenDataCalculator
{
  Calculate(IData)
}

class OnScreenData : IOnScreenData
{
  OnScreenData(PTR_T(IData), PTR_T(IOnScreenDataCalculator))

    enter code here

  GetOnScreenData()
}

现在假设我希望拥有多个用于不同类型的数据和计算器的工厂。我如何对这些工厂进行单元测试,我的工厂如下:

OnScreenBlueDataForWideScreenFactory
{
  PTR:T(IOnScreenData) Create()
  {
    PTR_T(Data) data = ptr_t(new BlueData());
    PTR_T(IOnScreenDataCalculator) calculator = ptr_t(new WideScreenDataCalculator());
    PTR_T(IOnScreenData) onScreenData = ptr_t(new WideScreenDataCalculator(data, calculator ));

    return onScreenData;
  }
}

谢谢你的帮助,

巴里。

I unit test my classes by giving all my classes an interface. These interfaces have in turn their own mocks.

But lets say I have the following:

class IData
{
  GetData()
}

class IOnScreenDataCalculator
{
  Calculate(IData)
}

class OnScreenData : IOnScreenData
{
  OnScreenData(PTR_T(IData), PTR_T(IOnScreenDataCalculator))

    enter code here

  GetOnScreenData()
}

Now lets say that I wish to have a number of factories for different types of data and calculators. How can I unit test these factories where my factories are as follows:

OnScreenBlueDataForWideScreenFactory
{
  PTR:T(IOnScreenData) Create()
  {
    PTR_T(Data) data = ptr_t(new BlueData());
    PTR_T(IOnScreenDataCalculator) calculator = ptr_t(new WideScreenDataCalculator());
    PTR_T(IOnScreenData) onScreenData = ptr_t(new WideScreenDataCalculator(data, calculator ));

    return onScreenData;
  }
}

Thanks for your help,

Barry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-10-23 12:45:41

我不确定代码片段是否真的是 C++,但示例应该是这样的:

class ExampleIface
{
  public:
    virtual ~ExampleIface() {}
    virtual void a() = 0;
};

class Example1: public ExampleIface
{
  public:
    virtual ~Example1() {}
    virtual void a()
    {
      // something
    }
};

class ExampleFactory
{
  public :
    typedef ExampleIface * ExamplePtrType; // can be shared_ptr instead

    static ExamplePtrType Create( /*params?*/)
    {
      ExamplePtrType p( new Example1 );
      return p;
    }

  private:
    ExampleFactory();
    ~ExampleFactory();
};

以及单元测试:

void test_Create()
{
  ExampleFactory::ExamplePtrType p = ExampleFactory::Create();
  Example1 *realType = dynamic_cast< Example1* >( p );
  TS_ASSERT( NULL != realType ); // if you use cxxtest
}

I am not sure the code snippets are really c++, but the example should be something like this :

class ExampleIface
{
  public:
    virtual ~ExampleIface() {}
    virtual void a() = 0;
};

class Example1: public ExampleIface
{
  public:
    virtual ~Example1() {}
    virtual void a()
    {
      // something
    }
};

class ExampleFactory
{
  public :
    typedef ExampleIface * ExamplePtrType; // can be shared_ptr instead

    static ExamplePtrType Create( /*params?*/)
    {
      ExamplePtrType p( new Example1 );
      return p;
    }

  private:
    ExampleFactory();
    ~ExampleFactory();
};

and the unit test:

void test_Create()
{
  ExampleFactory::ExamplePtrType p = ExampleFactory::Create();
  Example1 *realType = dynamic_cast< Example1* >( p );
  TS_ASSERT( NULL != realType ); // if you use cxxtest
}
南薇 2024-10-23 12:45:41

我将调用 Create() 并验证我是否获得了具有正确构成类型的正确构造的对象。

I'd call Create() and verify that I get a properly constructed object with the right constituent types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文