使用测试优先方法开发用户界面
2008-01-19 20:24:54 来源:WEB开发网核心提示:4、编写控件行为测试代码接下来应该是编写点击add button和delete button的测试代码了,同样,我们从TestCase继承出TestOperation:class TestOperation : public CppUnit::TestCase{ CPPUNIT_TEST_SUITE(TestOpera
4、编写控件行为测试代码
接下来应该是编写点击add button和delete button的测试代码了。同样,我们从TestCase继承出TestOperation:
class TestOperation : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(TestOperation);
CPPUNIT_TEST(testMovieList);
CPPUNIT_TEST(testAdd);
CPPUNIT_TEST(testDelete);
CPPUNIT_TEST_SUITE_END();
public:
void testMovieList();
void testAdd();
void testDelete();
public:
void setUp();
void tearDown();
TestOperation();
virtual ~TestOperation();
private:
static CString LOST_IN_SPACE;
CStringArray m_MovieNames;
MovieListWindow* m_pWindow;
MovieListEditor* m_pEditor;
};
你会发现,在TestOperation类中出现了一个成员变量MovieListEditor* m_pEditor。类MovieListEditor是一个用来保存影片数据以及对影片数据进行增加 ,删除操作的管理类。后面我们会给出它的实现。看看setUp()做了什么:
void TestOperation::setUp()
{
//创建一个MovieListEditor实例
m_pEditor = new MovieListEditor();
m_MovieNames.RemoveAll();
//将MovieListEditor中的影片列表拷贝到m_MovieNames,为后面测试作准备
for(int n=0; n<m_pEditor->GetMovies()->GetSize(); n++)
{
m_MovieNames.Add(m_pEditor->GetMovies()->GetAt(n));
}
}
我们来看看添加影片的测试,请看代码注释:
void TestOperation::testAdd()
{
//拷贝一份movie list
CStringArray MovieNamesWithAddition;
for(int n=0; n<m_MovieNames.GetSize(); n++)
{
MovieNamesWithAddition.Add(m_MovieNames.GetAt(n));
}
MovieNamesWithAddition.Add(LOST_IN_SPACE);
//生成窗口
MovieListWindow *pWindow = new MovieListWindow(m_pEditor);
pWindow->Init();
//填写新的影片的名称
CEdit* pEdit = pWindow->GetMovieField();
pEdit->SetWindowText(LOST_IN_SPACE);
//点击add btn
CButton* pBtn = pWindow->GetAddButton();
::SendMessage(pBtn->m_hWnd, BM_CLICK, 0, 0);
//检查列表控件中是否已加入新的影片
CListBox* pListBox = pWindow->GetMovieListBox();
CPPUNIT_ASSERT_EQUAL(MovieNamesWithAddition.GetSize(), pListBox->GetCount());
//检查列表控件中影片名是否正确
CString strNewMovieName;
pListBox->GetText(pListBox->GetCount()-1, strNewMovieName);
CPPUNIT_ASSERT_EQUAL(LOST_IN_SPACE, strNewMovieName);
//销毁窗口
pWindow->DestroyWindow();
delete pWindow;
pWindow = NULL;
}
更多精彩
赞助商链接