NMock 2.0 ちょっとだけ触ってみた (^o^)
シンタックスがとてもいい感じ。わかりやすい。
以下、使用方法を学習するために書いたテストコード
using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; using NMock2; namespace NMockStudy.Tests { [TestFixture] public class NMockTest { private IFoo target_; private Mockery mocks_; public interface IFoo { string SimpleHello(); string Hello(string message); string Message { get; } } [SetUp] public void SetUp() { mocks_ = new Mockery(); target_ = mocks_.NewMock<IFoo>(); } [Test] public void モックインスタンスが作成されているべき() { Assert.IsNotNull(target_); Assert.IsInstanceOfType(typeof(IFoo), target_); } [Test] public void パラメータなしのメソッドに対するエクスペクテーションが動作すべき() { Expect.Once.On(target_).Method("SimpleHello") .WithNoArguments().Will(Return.Value("Hello world")); Assert.AreEqual("Hello world", target_.SimpleHello()); } [Test] public void パラメータありのメソッドに対するエクスペクテーションが動作すべき() { Expect.Once.On(target_).Method("Hello") .With("world").Will(Return.Value("Hello world")); Assert.AreEqual("Hello world", target_.Hello("world")); } [Test] [ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void セットされていないパラメータでメソッドを呼び出すと例外が発生すべき() { Expect.Once.On(target_).Method("Hello") .With("world").Will(Return.Value("Hello world")); target_.Hello("tsune"); } [Test] [ExpectedException(typeof(NullReferenceException))] public void 例外をスローするエクスペクテーションが動作すべき() { Expect.Once.On(target_).Method("Hello") .With(new NMock2.Matchers.NullMatcher()) .Will(Throw.Exception(new NullReferenceException("ぬるぽ"))); target_.Hello(null); } [Test] [ExpectedException(typeof(NullReferenceException))] public void 上のはどうやらこう書くのが正しそうだ() { Expect.Once.On(target_).Method("Hello") .With(Is.Null) .Will(Throw.Exception(new NullReferenceException("ぬるぽ"))); target_.Hello(null); } [Test] public void ふつうのはこう書けるということか() { Expect.Once.On(target_).Method("Hello") .With(Is.EqualTo("world")).Will(Return.Value("Hello world")); Assert.AreEqual("Hello world", target_.Hello("world")); } [Test] public void 要するにIsのstaticなフィールドとメソッドはMatcherを返すんだな() { Assert.IsInstanceOfType(typeof(Matcher), Is.Anything); Assert.IsInstanceOfType(typeof(Matcher), Is.Nothing); Assert.IsInstanceOfType(typeof(Matcher), Is.NotNull); Assert.IsInstanceOfType(typeof(Matcher), Is.Null); Assert.IsInstanceOfType(typeof(Matcher), Is.Out); Assert.IsInstanceOfType(typeof(Matcher), Is.AtLeast(10)); Assert.IsInstanceOfType(typeof(Matcher), Is.AtMost(1)); Assert.IsInstanceOfType(typeof(Matcher), Is.EqualTo("hoge")); Assert.IsInstanceOfType(typeof(Matcher), Is.GreaterThan(1)); Assert.IsInstanceOfType(typeof(Matcher), Is.In(new int[3] { 1, 2, 3 })); Assert.IsInstanceOfType(typeof(Matcher), Is.LessThan(10)); Assert.IsInstanceOfType(typeof(Matcher), Is.OneOf(new int[3] { 1, 2, 3 })); Assert.IsInstanceOfType(typeof(Matcher), Is.Same(new object())); Assert.IsInstanceOfType(typeof(Matcher), Is.StringContaining("abcdefg")); } [Test] public void プロパティに対するエクスペクテーションも問題なく動作すべき() { Expect.Once.On(target_).GetProperty("Message") .Will(Return.Value("world")); Assert.AreEqual("world", target_.Message); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void Neverで指定したメソッドが呼び出されたら例外が例外が発生すべき() { Expect.Never.On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); } [Test] public void Neverで指定したメソッドは呼び出されるべきではない() { Expect.Never.On(target_).Method("SimpleHello").WithNoArguments(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void AtLeastで指定したメソッドが指定回数より少ない呼び出しでは例外が発生すべき() { Expect.AtLeast(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void AtLeastOnceで指定したメソッドは1回は呼び出されているべき() { Expect.AtLeastOnce.On(target_).Method("SimpleHello").WithNoArguments(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtLeastOnceで指定したメソッドは1回呼び出されていればよい() { Expect.AtLeastOnce.On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtLeastOnceで指定したメソッドは1回以上呼び出されていてもよい() { Expect.AtLeastOnce.On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtLeastで指定したメソッドは指定回数より多く呼び出されているべき() { Expect.AtLeast(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtLeastで指定したメソッドは指定回数だけ呼び出されていてもよい() { Expect.AtLeast(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void AtMostで指定したメソッドが指定回数より多い呼び出しでは例外が発生すべき() { Expect.AtMost(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtMostで指定したメソッドは指定回数より少なく呼び出されているべき() { Expect.AtMost(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void AtMostで指定したメソッドは指定回数だけ呼び出されていてもよい() { Expect.AtMost(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void Exactlyで指定したメソッドが指定回数より多い呼び出しでは例外が発生すべき() { Expect.Exactly(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void Exactlyで指定したメソッドが指定回数より少ない呼び出しでは例外が発生すべき() { Expect.Exactly(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void Exactlyで指定したメソッドは指定回数だけ呼び出されているべき() { Expect.Exactly(2).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void Betweenで指定したメソッドが指定回数の範囲より多い呼び出しでは例外が発生すべき() { Expect.Between(2, 3).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(NMock2.Internal.ExpectationException))] public void Betweenで指定したメソッドが指定回数の範囲より少ない呼び出しでは例外が発生すべき() { Expect.Between(2, 3).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } [Test] public void Betweenで指定したメソッドが指定回数の範囲で呼び出されているべき() { Expect.Between(2, 3).On(target_).Method("SimpleHello").WithNoArguments(); target_.SimpleHello(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); target_.SimpleHello(); mocks_.VerifyAllExpectationsHaveBeenMet(); } } }
こんなこともできるらしい。