Visual Studio Team System 2008にてテストプロジェクトを利用して、単体テストを行っています。
また、このエディションではコードカバレッジを取れるので100%にすることも要件にしています。
その中で見た目は処理が通っているのですが、カバレッジが100%にならないケースが出てきました。
以下がその例です。
- public void Method01(System.Windows.Forms.Control.ControlCollection list)
- {
- List<control> wk = new List<control>();
- foreach (Control tmp in list)
- {
- if (tmp is Label) continue;
- wk.Add(tmp);
- }
- }
public void Method01(System.Windows.Forms.Control.ControlCollection list)
{
List<control> wk = new List<control>();
foreach (Control tmp in list)
{
if (tmp is Label) continue;
wk.Add(tmp);
}
}
Collectionをforeachしているだけなのですが、これが問題のようです。
詳しい理由はわかりませんが、裏方さんのコードに通らないパスがあるっポイです。
とにかく、Collectionをforeachするとダメなので、Listをforeachするように変える処理を入れます。
-
- public List<t> ConvertList<t>(ICollection collection)
- {
- List<t> ret = new List<t>();
- IEnumerator tmp = collection.GetEnumerator();
-
- while(tmp.MoveNext()) {
- ret.Add((T)tmp.Current);
- }
- return ret;
- }
//CollectionをListに変換する
public List<t> ConvertList<t>(ICollection collection)
{
List<t> ret = new List<t>();
IEnumerator tmp = collection.GetEnumerator();
while(tmp.MoveNext()) {
ret.Add((T)tmp.Current);
}
return ret;
}
それで、foreachの所でこのメソッドを使います。
- public void Method01(System.Windows.Forms.Control.ControlCollection list)
- {
- List<control> wk = new List<control>();
- foreach (Control tmp in ConvertList<Control>(list))
- {
- if (tmp is Label) continue;
- wk.Add(tmp);
- }
- }
public void Method01(System.Windows.Forms.Control.ControlCollection list)
{
List<control> wk = new List<control>();
foreach (Control tmp in ConvertList<Control>(list))
{
if (tmp is Label) continue;
wk.Add(tmp);
}
}
これでカバレッジが100%になるようになりました。