Question

I'm writing a few CXX unit tests for a class I have written. I have never written CXX tests before (also this is the only framework I can use). I am trying to call a function contained within my class so that I can test the output is what is expected, but I am unsure if you can do this, and if so, how.

Test.h:
class Test..... {
    public:
        std::string GenerateHash(const std::string& user, const std::string& url) const;
    ...
}


Test.cpp:
string
Test::GenerateHash(const string& user, const string& urrl) const {
    ...
 }

I have included the header file in the CXX test, but cannot access the GenerateHash function.

Test.cxx_h:
void testCheckHashGeneration( void ) {
    TS_ASSERT_EQUALS (GenerateHash("testuser", "http://www.stackoverflow.com"), "DFEGEC.....");
}

Error: error: âGenerateHashâ was not declared in this scope

I also tried:

Test.cxx_h:
void testCheckHashGeneration( void ) {
    Test test;
    TS_ASSERT_EQUALS (test->GenerateHash("testuser", "http://www.stackoverflow.com"), "DFEGEC.....");
}

Error: error: âTestâ has not been declared 

Any help will be greatly appreciated.

Was it helpful?

Solution

In the first case, you're trying to call a member function without an instance of Test. You need a Test to call it on, as you've done in the second case.

In the second case, you're trying to call a member using the syntax for having a pointer to the object, '->', when you actually have an object. You want the second to look like this:

test.GenerateHash(...)

If you somehow had a Test*, then you could call it like

test->GenerateHash(...)

use . for objects, -> for pointers to objects.

More code and error output would be helpful.

OTHER TIPS

If the GenerateHash function can be a static method, then make it static. Then call it using Test::GenerateHash(/*your arguments*/);

Considering the way you initially attempted to call GenerateHash, I would guess that this should be a static method. If its execution does not depend on a particular instance of the Test class, then it can be static.

Otherwise, use test.GenerateHash rather than test->GenerateHash

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top