单元测试true和false的变量 [英] Unit test a variable for both true and false

查看:365
本文介绍了单元测试true和false的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用googletest / googlemock来处理C ++生产代码。我在使用A.cpp中的一个函数时遇到了这个想法:

  bool A :: process_ ); 

bool A :: process()
{
if(!process_){
process_ = true;
}
return process_;
}

其中标题包含:

  protected:
static bool process_;
public:
static bool process();

我卡住的方式,我只能测试函数的期望输出true或输入假如:

  TEST(ATest,processVal){

A a;
EXPECT_TRUE(a.process());

}

有一种方法来测试函数, code> * true * 和 * false * process _ ?所以测试覆盖了两者的决定。
我想嘲笑,所以它可以期望一个调用true和false,但因为它不是一个类的成员函数,我想你不能做嘲笑process_吗?
如果可以,如何模拟变量?



注意:我在linux和gcc :)谢谢! b

解决方案


  • 解决方案1:访问任何 protected



您可以轻松地修改 _process 即使受保护,只要这样做:

  class SubA:public A 
{
public:
void setProcess(bool val){process_ = val; }
};

A a;
SubA * pA =(SubA *)& a;
pA-> setProcess(false);

这将工作得很好,是安全的。即使你把 A * 转换为 SubA * 这不是真正有效,它将作为 SubA 对象与 A 对象相同( SubA



这只有工作原因,因为 process _ protected ,所以你创建一个派生类 SubA 并使用它,因为编译器将允许这个子类访问受保护的东西。




  • 解决方案2:要访问任何 protected ,甚至 $
    $ b $ c> process _
    private ,这不会工作...但是, private / protected 仅由编译器管理...属性在内存中,即使您不是允许也可以访问它们。



    这是丑陋,但工作完美:

      #define protected public 
    #define private public
    #includeAh
    #undef private
    #undef protected

    {
    A a;
    a._process = false;
    }

    这个黑客可以轻松访问任何 / code> / protected 属性/函数。


    I'm working on a C++ production code here with googletest/googlemock. I've stumble upon this idea when working with one of the function in A.cpp:

    bool A::process_(false);
    
    bool A::process()
    {
        if ( !process_ ){
            process_ = true;
        }
    return process_;
    }
    

    where the header contains:

    protected:
        static bool process_;
    public:
        static bool process();
    

    I am stuck in a way that I can only test the function for an expected output of true or an input of false like so:

    TEST(ATest, processVal){
    
        A a;
        EXPECT_TRUE(a.process());
    
    }
    

    Is there a way to test the function so that both *true* and *false* value of process_ is passed ? So that the test covers the decision for both. I was thinking mocking so that it can expect a call for both true and false but because it is not a function in a member of a class, I guess you can not do mocking for process_ ? If you can, how to mock the variable?

    Note: I'm on linux and gcc :) thank you !

    解决方案

    • Solution1: To access any protected attribute/method from a test without modifying production code

    You can easily modify _process even if protected, just do this:

    class SubA : public A
    {
    public:
        void setProcess( bool val ) { process_ = val; }
    };
    
    A a;
    SubA* pA = (SubA*) &a;
    pA->setProcess( false );
    

    This will work pretty well and is safe. Even if you are casting A* to SubA* which is not really valid, it will work as SubA objects in memory are the same as A objects (as SubA does not declare any extra attribute).

    This only works because process_ is protected, so you create a derived class SubA and use it because compiler will allow this subclass to access the protected stuff.

    • Solution2: To access any protected and even private attribute/method from a test without modifying production code

    Now, if process_ was private, this would not work...but, private/protected is only managed by the compiler...the attributes are here in memory and you may access them even if you are not "allowed to".

    It's ugly but works perfectly:

    #define protected public
    #define private public
    #include "A.h"
    #undef private
    #undef protected
    
    {
        A a;
        a._process = false;
    }
    

    This hack makes it easy to access any private/protected attributes/functions from your test programs.

    这篇关于单元测试true和false的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆