mardi 29 mars 2016

Accessing Variable of an Instanced Class

Firstly, I apologise for the bad title naming sense. I am unsure as to how to phrase it correctly.

My problem is that I have four given classes, which I call A, B, C and D for simplicity.

  • A contains a method which calls B to do something.
  • B is a singleton, and provides A with an instance of it.
  • B has a variable, C* c.
  • C is basically a table containing many D.

I want to test that D has the correct information stored in it, passed through to the program via A.

In code form, it would look something like this (Note that they are originally separated into .h and .cpp, but I combined them and extracted relevant information, so it may seem weird):

Class A:

#pragma once

#include "B.h"

public:
    bool someFunction(std::string sampleString) {
        B::getInstance()->doSomething(sampleString);
    }

Class B:

#pragma once

#include "C.h"

private:
    static B* instance;
    C* c;

public:
    static B* getInstance();

    bool doSomething(std::string sampleString) {
        // Here, I assume that C only contains one D, so dNum is actually 1
        // In reality, the sampleString contains dNum to be extracted
        int dNum = 1;
        neededD = c->getD(dNum);

        // saves data from sampleString in neededD
    }

Class C:

#pragma once

#include "D.h"

private:
    std::vector<D*> d;

public:
    D* getD(int dNum) {
        D* required D;
        requiredD = d->at(dNum);
        return d;
    }

Class D:

#pragma once

private:
    // A bunch of vectors to store data about a particular D

public:
    // A bunch of get/set methods for the vectors
    // one of them is getString()

I am supposed to call the method in A, and check that D contains the correct data which I have passed into the method in A. However, I am completely unsure as to how to do this.

I have tried the following in my test file:

#include "A.h"

public:
    A* a;
    B* b;
    C* c;

    TEST_METHOD(Test1) {
        std::string testString = "abc";
        a->someFunction(testString);

        // the following doesn't work
        std::string checkString = (c->getD(1))->getString();
        Assert::IsTrue(testString == checkString);
    }

I don't even understand what I am typing in the test method I gave above with regards to accessing C to get the D I want, but I hope it provides some explanation on what I'm trying to achieve here. Basically, to test that the D has the correct information I passed into A.

I have taken a look at stubs and mocks, but I do not understand how to use them in this situation (I have actually never utilised them).

Once again, sorry if my explanation isn't clear. I am very weak at C++, and thus do not really know how to explain things. As such, even searching for similar questions proved to be impossible, so I apologise if this question has been asked before.

Thank you very much for any assistance you may provide!

Aucun commentaire:

Enregistrer un commentaire