On Sunday, 2014-02-16, 06:38:35, Michael Stemle wrote:
> So, I'm still getting used to the dpointer paradigm. Is anybody familiar
> with how to use QTest to simulate code paths dependent on values in the
> dpointer which have no exposed mutators?
> 
> For instance, I have QApt::Transaction which has its dpointer, and its
> dpointer has a property "error". This property is exposed via a Q_PROPERTY
> macro in the public class, but it's read-only. I'm adding error handling
> and I'd like to add some unit tests for the error handling, but I'm having
> trouble figuring out how to force an instance of QApt::Transaction into
> executing that error handling without violating encapsulation or adding a
> klugy method just for my test.

How about this:

Put the declaration of the Private in a header, say yourclass_p.h
This header does not get installed but can be included by the test.

Add a protected constructor to your class that takes an instance of the 
Private

class MyClass
{
protected:
    explicit MyClass(MyClassPrivate *d) : d_ptr(d) {} // maybe assert on !d 
};

Your test can the do something like

MyClassPrivate *dTest = new MyClassPrivate;

class MyClassTest : public MyClass
{
public:
    MyClassTest() : MyClass(dTest)  {}
};

Cheers,
Kevin
-- 
Kevin Krammer, KDE developer, xdg-utils developer
KDE user support, developer mentoring

Attachment: signature.asc
Description: This is a digitally signed message part.

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

Reply via email to