Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InSequence doesn't work on Windows when GMock linked as shared library #3412

Closed
vgaraschuk opened this issue May 21, 2021 · 4 comments
Closed
Assignees

Comments

@vgaraschuk
Copy link

InSequence has no effect on Windows if gtest and gmock are linked as shared libraries (DLL). The same test works as expected if run on Linux.
Test code:

#include "gmock/gmock.h"
#include "gtest/gtest.h"

class MockI
{
public:
    virtual void method1() = 0;
    virtual void method2() = 0;
};

class Mock : public MockI
{
public:
    MOCK_METHOD(void, method1, (), (override));
    MOCK_METHOD(void, method2, (), (override));
};

TEST(Test, Test)
{
    Mock* mock = new Mock();

    ::testing::InSequence s;
    EXPECT_CALL(*mock, method1);
    EXPECT_CALL(*mock, method2);

    mock->method2();
    mock->method1();

    ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(mock));

    delete mock;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.12)

project("Test")

set(target_name Test)

add_executable(${target_name} test.cpp)

target_compile_definitions(${target_name} PRIVATE GTEST_LINKED_AS_SHARED_LIBRARY=1)
target_include_directories(${target_name} PRIVATE "${CMAKE_SOURCE_DIR}/external/include")
target_link_directories(${target_name} PRIVATE "${CMAKE_SOURCE_DIR}/external/lib.win/${CMAKE_BUILD_TYPE}")
target_link_libraries(${target_name} PRIVATE gtest gmock gtest_main)

file(GLOB_RECURSE externals "${CMAKE_SOURCE_DIR}/external/bin.win/${CMAKE_BUILD_TYPE}/*")
file(COPY ${externals} DESTINATION ${CMAKE_BINARY_DIR})

The test passes without an error even though the methods were called in reverse order.

@sebkraemer
Copy link
Contributor

@vgaraschuk Could you try with only linking gmock (and gmock_main, eventually) and leave out the gtest* libraries in your link command?

gmock should include gtest statically, I believe, and using both gmock and gtest dynamically is reported (suspected) to have unexpected behavior. See e.g. this issue.

@vgaraschuk
Copy link
Author

@sebkraemer , Thank you for your reply. I just tried to only link gmock and gmock_main to the sample I provided - the outcome is the same.

@sebkraemer
Copy link
Contributor

sebkraemer commented Nov 3, 2021

Looks like even the *_main libraries contains all necessary code (see here so even reducing further might be the way to go).
Not sure how main() in the dll is supposed to work. I'd use the gmock.dll and write the main() myself, calling the necessary initialisation code and RUN_ALL_TESTS().

@vgaraschuk
Copy link
Author

@sebkraemer , That actually fixed the issue. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants