Adding custom interfaces to your mock instance.

Posted by mehfuzh on ASP.net Weblogs See other posts from ASP.net Weblogs or by mehfuzh
Published on Wed, 09 Jun 2010 14:57:52 GMT Indexed on 2010/06/09 15:02 UTC
Read the original article Hit count: 367

Filed under:
|
|
|

Previously, i made a post  showing how you can leverage the dependent interfaces that is implemented by JustMock during the creation of mock instance. It could be a informative post that let you understand how JustMock behaves internally for class or interfaces implement other interfaces into it. But the question remains, how you can add your own custom interface to your target mock. In this post, i am going to show you just that.

Today, i will not start with a dummy class as usual rather i will use two most common interfaces in the .NET framework  and create a mock combining those. Before, i start i would like to point out that in the recent release of JustMock we have extended the Mock.Create<T>(..) with support for additional settings though closure. You can add your own custom interfaces , specify directly the real constructor that should be called or even set the behavior of your target.

Doing a fast forward directly to the point,  here goes the test code for create a creating a mock that contains the mix for ICloneable and IDisposable using the above mentioned changeset.

  1. var myMock = Mock.Create<IDisposable>(x => x.Implements<ICloneable>());
  2. var myMockAsClonable = myMock as ICloneable;
  3. bool isCloned = false;
  4.  
  5. Mock.Arrange(() => myMockAsClonable.Clone()).DoInstead(() => isCloned = true);
  6.  
  7. myMockAsClonable.Clone();
  8.  
  9. Assert.True(isCloned);

 

Here, we are creating the target mock for IDisposable and also implementing ICloneable. Finally, using the “as” for getting the ICloneable reference accordingly arranging it, acting on it and asserting if the expectation is met properly.

This is a very rudimentary example, you can do the same for a given class:

  1. var realItem = Mock.Create<RealItem>(x =>
  2. {
  3.     x.Implements<IDisposable>();
  4.     x.CallConstructor(() => new RealItem(0));
  5. });
  6. var iDispose = realItem as IDisposable;
  7.    
  8. iDispose.Dispose();

Here, i am also calling the real constructor for RealItem class.  This is to mention that you can implement custom interfaces only for non-sealed classes or less it will end up with a proper exception. Also, this feature don’t require any profiler, if you are agile or running it inside silverlight runtime feel free to try it turning off the JM add-in :-).

TIP :  Ability to  specify real constructor could be a useful productivity boost in cases for code change and you can re-factor the usage just by one click with your favorite re-factor tool.

 

That’s it for now and hope that helps

Enjoy!!

© ASP.net Weblogs or respective owner

Adding custom interfaces to your mock instance.

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Wed, 09 Jun 2010 00:00:00 GMT Indexed on 2010/06/09 16:53 UTC
Read the original article Hit count: 367

Filed under:
Previously, i made a post  showing how you can leverage the dependent interfaces that is implemented by JustMock during the creation of mock instance. It could be a informative post that let you understand how JustMock behaves internally for classes or interfaces implement other interfaces into it. But the question remains, how you can add your own custom interface to your target mock. In this post, i am going to show you just that. Today, i will not start with a dummy class as usual rather...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

Related posts about c#

Related posts about telerik