Being Cellfish

Stuff I wished I've found in some blog (and sometimes did)

Evolution of a hand rolled fake - part 2B

Change of Address
This blog has moved to blog.cellfish.se.

Evolution of a hand rolled fake - part 2B

  • Comments 1

While the last version I showed is very flexible I have experimented a little more. Why would you ever have a public getter on the fake handler properties? Maybe if you had a fake that needed to wrap existing fake behavior but this is probably pretty rare. So this is what it would look like with no public getters. Again this is just to declutter the fake API surface.

 1: public class FakeTheInterface : ITheInterface
 2: {
 3:     private Action<int> doSomething;
 4:  
 5:     private Func<int, int, int> computeSomething;
 6:  
 7:     public Action<int> DoSomething
 8:     {
 9:         set { this.doSomething = value; }
 10:     }
 11:  
 12:     public Func<int, int, int> ComputeSomething
 13:     {
 14:         set { this.computeSomething = value; }
 15:     }
 16:  
 17:     void ITheInterface.DoSomething(int x)
 18:     {
 19:         Assert.IsNotNull(this.doSomething, "Unexpected call to DoSomething");
 20:         this.doSomething(x);
 21:     }
 22:  
 23:     int ITheInterface.ComputeSomething(int a, int b)
 24:     {
 25:         Assert.IsNotNull(
 26:             this.computeSomething, "Unexpected call to ComputeSomething");
 27:         return this.computeSomething(a, b);
 28:     }
 29: }
  • Instead of having explicit private fields you can have { private get; set; } property:

           public Action<int> DoSomething { private get; set; }

           public Func<int, int, int> ComputeSomething { private get; set; }

Page 1 of 1 (1 items)