Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating mockobject of Qfile by replacing Qbuffer possible?
Qt 6.11 is out! See what's new in the release blog

Creating mockobject of Qfile by replacing Qbuffer possible?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 546 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChiTor
    wrote on last edited by
    #1

    Hi ya'll,

    For the unit testing purpose, I have a QFile writing to and reads from a file. However, I need to replace this file as a memory, such as QBuffer, since they are from the same base class QIODevice, so it won't be necessary to create the file to perform a unit test.

    How can I write a method with QFile so it would write to Qbuffer or QByteArray, so I could verify it's contents acting like in memory instead? Likewise, for the other way around reading a Qbuffer from QFile.

    What's the best way to approach this? It may require some advanced OOP with inheritance, but I'm not sure it will work.

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi,

      One way is to use QIODevice rather than QFile for your method.

      That way you can pass whatever you want for your tests.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Don't use QFile but your own class which either routes to a QFile or QBuffer/QByteArray.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi,

          One way is to use QIODevice rather than QFile for your method.

          That way you can pass whatever you want for your tests.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • C Offline
            C Offline
            ChiTor
            wrote on last edited by
            #4

            Thank you so much for explaining! It works to pass QFile and QBuffer like that.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved