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."); } };
Gebb
source share