NUnit Codition系のAssertion
昨日に引き続きNUnitの調査。
今日は、IsTrue, IsFalseなどのCondition系のアサーションです。
NUnitの新機能というわけじゃないですけど、
利用頻度の低いメソッドも多いので復習も兼ねてます。
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using NUnit.Framework; namespace NAgile.Sample.NUnit { [TestFixture] public class ConditionTest { [Test] public void IsNaNメソッドを試してみるか() { Assert.IsNaN(double.NaN); } [Test] [ExpectedException(typeof(AssertionException))] public void IsNaNメソッドに数値を渡すと例外を返すよね() { Assert.IsNaN(0); } [Test] public void IsEmptyメソッドも試してみよう() { Assert.IsEmpty(""); Assert.IsEmpty(string.Empty); } [Test] [ExpectedException(typeof(AssertionException))] public void IsEmptyメソッドにNULLを渡してみると例外を返す() { string nullString = null; Assert.IsEmpty(nullString); } [Test] public void IsEmptyメソッドはコレクションも調べられるんだって() { StringCollection targetCollection = new StringCollection(); Assert.IsEmpty(targetCollection); } [Test] [ExpectedException(typeof(AssertionException))] public void IsEmptyメソッドに空じゃないコレクションを渡すと例外を返すよね() { StringCollection targetCollection = new StringCollection(); targetCollection.Add("コレクションの中身1"); Assert.IsEmpty(targetCollection); } [Test] public void ついでにIsNotEmptyメソッドも試してみよう() { Assert.IsNotEmpty("これは空じゃない文字列です"); } [Test] public void IsNotEmptyメソッドにNULLを渡してみると通ります() { string nullString = null; Assert.IsNotEmpty(nullString); } [Test] public void IsNotEmptyメソッドはコレクションが空じゃないか調べられるんだって() { StringCollection targetCollection = new StringCollection(); targetCollection.Add("コレクションの中身1"); Assert.IsNotEmpty(targetCollection); } [Test] [ExpectedException(typeof(AssertionException))] public void IsNotEmptyメソッドに空のコレクションを渡すと例外を返すよね() { StringCollection targetCollection = new StringCollection(); Assert.IsNotEmpty(targetCollection); } } }
注意しなきゃいけないなと思ったのは、IsNotEmptyメソッドはNullを通すという点ですね。
Nullを空という意味合いでコーディングしてると、問題が起きそうです。
事前にNullの使用方法も確認しとかなあかんですね。