10.07.2015 Views

Unit Testing a C++ Database Appli Mock Objects Mock ... - PNSQC

Unit Testing a C++ Database Appli Mock Objects Mock ... - PNSQC

Unit Testing a C++ Database Appli Mock Objects Mock ... - PNSQC

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Unit</strong> <strong>Testing</strong> a <strong>C++</strong> <strong>Database</strong> <strong>Appli</strong>cation with<strong>Mock</strong> <strong>Objects</strong>Ray Lischner<strong>PNSQC</strong>October 11, 2011___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 1


Typical Usage (Simplified)struct Name { std::string first, last };typedef std::vector Names;class DbApp{public:DbApp(db::sql* sql);void get_names(std::string last_name, Names& name);private:static std::string query_;std::auto_ptr sql_;std::auto_ptr stmt_;}___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 3


Typical Usage (Simplified)std::string DbApp::query_("SELECT * FROM names WHERE last LIKE ?");DbApp::DbApp(db::sql* sql): sql_(sql),stmt_(sql->prepare(query_)){}void DbApp::get_names(std::string last_name, Names& names) {stmt_->bind_param(last_name);std::string first, last;stmt_->bind_result(first, last);stmt_->execute();while (stmt_->fetch())names.push_back(Name(first, last));}___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 4


<strong>Unit</strong> Test Error Casesdb::mock::sql sql;sql.store_result(DbApp::query_, db::mock::error);DbApp dbapp(&sql);Names names;BOOST_CHECK_THROW(dbapp.get_names("name", names),db::exception);___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 6


Impure Abstract Interfaceclass statement {public:templatevoid bind_param(T1 const& arg1, T2 const& arg2) {params p;p.push_back(param(arg1));p.push_back(param(arg2));bind_params(p);}private:void bind_params(params const& p) = 0;};___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 9


Real and <strong>Mock</strong> Classessqlmysql::sqlmock::sqlstatementmysql::statementmock::statement___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 10


libdbDesigned for unit testing<strong>Mock</strong> and real libraries designed together___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 12


Questions___________________________________________________________________________________________________________________________Copies may not be made or distributed for commercial use Excerpt from <strong>PNSQC</strong> 2011 <strong>PNSQC</strong>.ORG 13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!