NSubstitute
NUnitのモックを使いこなせるようになってみようと思ってやってみた。 NUnit.mocks.DynamicMock を使ったら、警告が出た。
警告 1 'NUnit.Mocks.DynamicMock' は古い形式です: 'NUnit now uses NSubstitute'
というわけで、NSubstituteを使ってみた。
公式サイト(GitHub)
公式サイト(nuget)
http://www.nuget.org/packages/NSubstitute
NUGet以外のインストール方法
- DLLをダウンロードする。 上記の公式サイト(github)にある「Download」でDLLが入ったzipファイルを取得できる。
- 解凍して、NSubstitute.dll をテストプロジェクトに置く。
- 参照設定に追加する。
- using NSubstitute; を追加する。
基本的な使い方
http://nsubstitute.github.io/help/getting-started/
- モックオブジェクトを作る。
- モックオブジェクトに、どう呼び出したらどう振舞うかを設定する。
- 使う。
例
public interface ICalculator
{
int Add(int a, int b);
string Mode { get; set; }
event EventHandler PoweringUp;
}
calculator.Add(1, 2).Returns(3); Assert.That(calculator.Add(1, 2), Is.EqualTo(3)); => モックの挙動をテストしてみた。
その他の使い方
このドキュメントに全部書いてある。
http://nsubstitute.github.io/help.html
//Check a call was received/ not received. calculator.Add(1, 2); calculator.Received().Add(1, 2); => アサーション成功 calculator.DidNotReceive().Add(5, 7); => アサーション失敗
メモ モックとスタブの違い
このサイトでズバリ、分かりやすい。感謝です。