eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.24k stars 170 forks source link

Can't invoke mock instances as passed arguments #101

Open williamkibira opened 7 years ago

williamkibira commented 7 years ago

Hello , i have been trying to verify method calls being invoked inside a class that i am passing the mocked instances to as you can see in the code i have put below in my test case


#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file

#include "catch.hpp"
#include "src/auth_presenter.h"
#include "src/repository/auth_repository.h"
#include "fakeit/single_header/catch/fakeit.hpp"

using namespace fakeit;

TEST_CASE( "Authentication Presenter Tests on MVP Pattern", "[Auth Presenter]" ) {
    Mock<AuthRepository> mockRepository;
    Mock<AuthContract::View> mockView;

    SECTION( "normal Authentication" ) {

        // User user;
        // user.first_name = "Zhang";
        // user.last_name = "Kun";

        When(Method(mockRepository, authCredentials).Using(Any<std::string>(), Any<std::string>(), Any<std::string>())).Return(new User);

        AuthPresenter authPresenter(&mockRepository.get(), &mockView.get());  
        std::string username = "TEST_NAME";
        std::string password = "TEST_PWD";
        authPresenter.login(username, password);

        Verify(Method(mockRepository, authCredentials)).Once();
        Verify(Method(mockView, on_success)).Once();
    }

    SECTION( "Authentication Cancellation" ) {
    Fake(Method(mockView, on_cancel));
    AuthPresenter authPresenter(&mockRepository.get(), &mockView.get());
    authPresenter.cancel();
    Verify(Method(mockView, on_cancel)).Once();

    }

}

I keep getting the following error when i run the tests

explicitly with message: Unexpected method invocation: unknown() An unmocked method was invoked. All used virtual methods must be stubbed!

=============================================================================== test cases: 1 | 1 failed assertions: 1 | 1 failed

Any ideas on the way to get the test to detect method invocation ?

j2bbayle commented 7 years ago

It means that the method unknown() has been called on one of your mock objects, and you haven't stubbed nor faked it. Try to add one of the following lines:

Fake(Method(mockRepository, unknown));
Fake(Method(mockView, unknown));
williamkibira commented 7 years ago

Wow , that a did the trick.

Thanks a lot for everything .