Thursday, June 08, 2006

using NUnit.Mocks DynamicMock with a Property in C#

Starting to use the DynamicMock feature of NUnit.Mocks. I had a class with a property getter that I wanted to use it with.  The code consistently failed.  Grabbed the NUnit source and looked at the covering unit test for DynamicMock.  Guess what, no covering test for a Property to show me how to do it!  I started thinking about it and realized that Property getters and setters are just treated as speciallly named methods under the covers and started playing around.  Finally came up with the following that works:

    8 namespace DynamicMockTest

    9 {

   10     [TestFixture]

   11     public class Class1

   12     {

   13 

   14         [Test]

   15         public void Test1()

   16         {

   17             DynamicMock mock = new DynamicMock(typeof(IMockTest));

   18             mock.ExpectAndReturn("get_Domain", "AMSWORLD");

   19 

   20             Assert.AreEqual("AMSWORLD", ((IMockTest)mock.MockInstance).Domain);

   21         }

   22 

   23     }

   24 

   25 

   26     public interface IMockTest

   27     {

   28         string Domain { get; }

   29     }

   30 }

 

The trick is to precede the property name with get_ or set_

 

Thursday, June 08, 2006 2:17:45 PM (Pacific Standard Time, UTC-08:00)   #      Comments [3]  
 

Monday, June 19, 2006 5:22:10 AM (Pacific Standard Time, UTC-08:00)
Glad you posted this - just saved me some time - I was encountering the same issue.

Interesting that other demo's don't use the get_ set_ either.

Change in version perhaps?
Friday, July 14, 2006 1:06:41 AM (Pacific Standard Time, UTC-08:00)
Glad to read such a nice piece of information.
Tuesday, April 24, 2007 6:08:13 PM (Pacific Standard Time, UTC-08:00)
I have used the get_ ans set_ prefixes, but what happens if you want to test internally, a method that has such a naming convention, and it isn't a property? Does that mean that NUnit.Mocks won't work?
Comments are closed.

Administration
Sign In