Skip to content

Instantly share code, notes, and snippets.

@biot023
Created September 8, 2012 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biot023/3673595 to your computer and use it in GitHub Desktop.
Save biot023/3673595 to your computer and use it in GitHub Desktop.
namespace persistence {
using ::testing::Invoke;
template <typename T>
struct MockCreator : public Creator<T>
{
typedef map<string, Variant> values_map_t;
MockCreator( shared_ptr<const T> object, shared_ptr<IConnection> connection )
: Creator<T>( object, connection )
{
ON_CALL( *this, object() ).
WillByDefault( Invoke( this, &MockCreator::__super_object ) );
ON_CALL( *this, connection() ).
WillByDefault( Invoke( this, &MockCreator::__super_connection ) );
}
MOCK_METHOD0_T( __function_operator, void() );
virtual void operator() () { return __function_operator(); }
MOCK_CONST_METHOD0_T( types_name, const string &() );
MOCK_CONST_METHOD0_T( values, shared_ptr<const values_map_t>() );
MOCK_CONST_METHOD0_T( object, shared_ptr<const T>() );
MOCK_CONST_METHOD0_T( connection, shared_ptr<const IConnection>() );
MOCK_METHOD0_T( _create_object, void() );
MOCK_METHOD0_T( _create_child_objects, void() );
MOCK_METHOD0_T( _create_child_collections, void() );
private:
shared_ptr<const T> __super_object() { return Creator<T>::object(); }
shared_ptr<const IConnection> __super_connection() { return Creator<T>::connection(); }
};
}
namespace {
namespace creating_object {
using persistence::IConnection;
using persistence::Creator;
using persistence::MockCreator;
using ::testing::Test;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::NiceMock;
template <typename T>
struct PartMockCreator : public MockCreator<T>
{
PartMockCreator( shared_ptr<const T> object, shared_ptr<IConnection> connection )
: MockCreator<T>( object, connection )
{
ON_CALL( *this, _create_object() ).
WillByDefault( Invoke( this, &PartMockCreator::__super_create_object ) );
}
private:
void __super_create_object() { return Creator<T>::_create_object(); }
};
struct CreatorCreatingObjectTest : public Test
{
protected:
shared_ptr<MockObject> _object;
shared_ptr<MockConnection> _connection;
NiceMock<PartMockCreator<MockObject>> _creator;
string _types_name;
CreatorCreatingObjectTest() :
_object( make_shared<MockObject>() ),
_connection( make_shared<MockConnection>() ),
_creator( _object, _connection ),
_types_name( "bendy_willies" )
{
ON_CALL( _creator, types_name() ).WillByDefault( Return( _types_name ) );
}
virtual void operator() () {
_creator._create_object();
}
};
// Should get the types name
TEST_F( CreatorCreatingObjectTest,
ShouldGetTheTypesName ) {
EXPECT_CALL( _creator, types_name() );
( *this )();
}
// Should get the values to persist
// Should use the connection to insert values with types name
// Should set the object's persistence id to insertion value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment