2013-04-11 10 views
6

Sto iniziando a utilizzare googlemock con googletest ma sto ottenendo un'eccezione SEH che non riesco a capire.SEH eccezione quando si utilizza googlemock

Il messaggio di errore è:

unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. 

Ho letto alcune domande simili su SO e altrove, ma sono ancora trovare una risposta per un semplice esempio tale.

vale a dire questo sta accadendo sul mio codice reale, ma ho anche riprodotto l'errore nel semplicissimo esempio di seguito. Sto costruendo con MSVC2008.

codice che riproduce l'errore:

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

#include <iostream> 

using testing::Exactly; 

class Production 
{ 
public: 
    virtual ~Production() {}; 
    virtual void fn() = 0; 
}; 

class ProductionCode : public Production 
{ 
public: 
    virtual ~ProductionCode() {}; 
    void fn() 
    { 
     std::cout << "CALLED ProductionCode::fn" << std::endl; 
    } 
}; 

class MockProduction : public Production 
{ 
public: 
    virtual ~MockProduction() {}; 
    MOCK_METHOD0(fn, void()); 
}; 

class ProductionUser 
{ 
public: 
    void methodUnderTest(Production *p) 
    { 
     p->fn(); 
    } 
}; 

TEST(ProductionTest, CallTheProductionFunction) { 
    ProductionCode p; 

    ASSERT_NO_THROW(p.fn()); 
} 

TEST(ProductionTest, CallTheMethodUnderTest) { 
    Production* p = new ProductionCode; 
    ProductionUser u; 

    ASSERT_NO_THROW(u.methodUnderTest(p)); 

    delete p; 
} 

TEST(ProductionTest, CallTheMethodUnderTestWithMock) { 
    MockProduction m; 

    EXPECT_CALL(m, fn()) 
     .Times(Exactly(1)); 

    ProductionUser u; 
    ASSERT_NO_THROW(u.methodUnderTest(&m)); 
} 

la mia uscita di prova dalla console:

[==========] Running 3 tests from 1 test case. 
[----------] Global test environment set-up. 
[----------] 3 tests from ProductionTest 
[ RUN  ] ProductionTest.CallTheProductionFunction 
CALLED ProductionCode::fn 
[  OK ] ProductionTest.CallTheProductionFunction (4 ms) 
[ RUN  ] ProductionTest.CallTheMethodUnderTest 
CALLED ProductionCode::fn 
[  OK ] ProductionTest.CallTheMethodUnderTest (2 ms) 
[ RUN  ] ProductionTest.CallTheMethodUnderTestWithMock 
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. 

[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock (0 ms) 
[----------] 3 tests from ProductionTest (10 ms total) 

[----------] Global test environment tear-down 
[==========] 3 tests from 1 test case ran. (13 ms total) 
[ PASSED ] 2 tests. 
[ FAILED ] 1 test, listed below: 
[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock 

1 FAILED TEST 

.\simple.cpp(59): ERROR: this mock object (used in test ProductionTest.CallTheMe 
thodUnderTestWithMock) should be deleted but never is. Its address is @000000000 
014F800. 
ERROR: 1 leaked mock object found at program exit. 
Press any key to continue . . . 

Sto usando il mio propria funzione principale come segue:

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

int main(int argc, char** argv) { 
    // The following line must be executed to initialize Google Mock 
    // (and Google Test) before running the tests. 
    ::testing::InitGoogleMock(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

Sono indovinando che sto facendo un errore piuttosto semplice qui, qualcuno può vedere dove sto andando male? Grazie!

[originale a cura di rendere il codice di uscita partita & console]

+0

È questo il codice effettivo che genera quell'output? Non vedo 'ProductionTest.CallTheUseOnProductionUser()' e due 'ProductionTest.CallTheMethodUnderTest()' s. – metal

+0

@metal È lì. 'TEST (ProductionTest, CallTheMethodUnderTest)' è una macro che specifica il test unitario che viene automaticamente chiamato buy the testing framework. –

+0

Vedo quel test elencato due volte e nessun segno di 'CallTheUseOnProductionUser'. Devo ammettere che non ho familiarità con GoogleMock, ma ho utilizzato molti altri framework di test. Mi sto perdendo qualcosa? – metal

risposta

3

Ho incontrato lo stesso problema quando ho compilato il gmock come DLL e collegati in un altro progetto. Dopo un sacco di prova, ho trovato il motivo è:

È necessario compilare il gmock e il vostro progetto nella stessa configurazione!

Ciò significa che è necessario compilare il gmock nella configurazione DEBUG (RELEASE), se si desidera collegarlo nella modalità DEBUG (RELEASE). In caso contrario, il file

sconosciuto: errore: eccezione SEH con codice 0xc0000005 generato nel corpo del test.

si verifica sempre.

Spero che la mia esperienza possa aiutarti, anche se potresti incontrare questo problema in una scena diversa.

+1

Sto vedendo un problema simile anche se è specificamente con gtest. Il mio progetto costruisce, collega ed esegue in modalità di rilascio solo bene, ma non in modalità di debug. Non sono sicuro di quale sia il problema, visto che ho creato gtest in entrambe le modalità e CMake trova la libreria gtest appropriata in base al mio tipo di build.Un altro commento sarebbe di grande aiuto –

+0

Questo mi ha aiutato su. –

2

Penso che si potrebbe forzare GTEST non non cloack l'eccezione esatto (quello che potrebbe essere fatto utilizzando il codice come

::testing::GTEST_FLAG(catch_exceptions) = false; 

o dalla riga di comando) E se poi si utilizza un debugger, è' Prenderò facilmente la pila. O anche se non lo fai, mi aspetto un SO * nix per scrivere il file core per poterlo analizzare in un secondo momento.