IDependency dependency = mockery.NewMock<IDependency>();
This will create a new dynamic mock for the interface IDependency
.
You can use the non-generic version of the NewMock()
method in .NET 1.0/1.1:
IDependency dependency = (IDependency)mockery.NewMock(typeof(IDependency));
You can give mocks a name that will be returned on calls to ToString()
.
This is useful to get better descriptions in exception and log code while unit testing or
because you need to mock the ToString()
method.
IDependency dependency = mockery.NewMock<IDependency>("name of mock");
[TestFixture] public void NamedMockTest { private Mockery mockery; [SetUp] public void SetUp() { mockery = new Mockery(); } [Test] public void NamedMock() { IDependency dependency = mockery.NewMock<IDependency>("name of mock"); Assert.AreEqual("name of mock", dependency.ToString()); } }