How to use moles for constructor?

I have a class like this:

public class Product : IProduct { static private string _defaultName = "default"; private string _name; private float _price; /// Constructor public Product() { _price = 10.0F; } public void ModifyPrice(float modifier) { _price = _price * modifier; } 

I want ModifyPrice to do nothing for a specific value, but I also want to call the constructor, which set the price to 10. I tried something like this:

 var fake = new SProduct() { CallBase = true }; var mole = new MProduct(fake) { ModifyPriceSingle = (actual) => { if (actual != 20.0f) { MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual)); } } }; MProduct.Constructor = (@this) => (@this) = fake; 

But even if the fake is well initialized with a good constructor, I cannot assign it to @this. I also try something like

 MProduct.Constructor = (@this) => { var mole = new MProduct(@this)... }; 

But this time I can’t name my constructor. How am I supposed to do?

+7
source share
2 answers

You don’t need to scoff at the constructor, the dimensionless constructor of the Product class already does what you want.

Add debug output to Product .

 public class Product { private float _price; public Product() { _price = 10.0F; Debug.WriteLine("Initializing price: {0}", _price); } public void ModifyPrice(float modifier) { _price = _price*modifier; Debug.WriteLine("New price: {0}", _price); } } 

Check only the ModifyPrice method.

 [TestMethod] [HostType("Moles")] public void Test1() { // Call a constructor that sets the price to 10. var fake = new SProduct { CallBase = true }; var mole = new MProduct(fake) { ModifyPriceSingle = actual => { if (actual != 20.0f) { MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual)); } else { Debug.WriteLine("Skipped setting price."); } } }; fake.ModifyPrice(20f); fake.ModifyPrice(21f); } 

See the debug output for confirmation that everything is working as expected:

  Initializing price: 10
     Skipped setting price.
     New price: 210

By the way, you don’t need to use a stub here,

 var fake = new SProduct { CallBase = true }; 

creating an instance of Product will suffice.

 var fake = new Product(); 

Update: Extruding a single method can be accomplished with an AllInstances class like this

 MProduct.Behavior = MoleBehaviors.Fallthrough; MProduct.AllInstances.ModifyPriceSingle = (p, actual) => { if (actual != 20.0f) { MolesContext.ExecuteWithoutMoles(() => p.ModifyPrice(actual)); } else { Debug.WriteLine("Skipped setting price."); } }; // Call the constructor that sets the price to 10. Product p1 = new Product(); // Skip setting the price. p1.ModifyPrice(20f); // Set the price. p1.ModifyPrice(21f); 
+5
source
 MProduct.Behavior = MoleBehaviors.Fallthrough; 
0
source

All Articles