如何使用Moles作为构造函数?
|
我有这样的课:
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;
}
我希望ModifyPrice对特定值不执行任何操作,但我也想调用将价格设置为10的构造函数。我尝试了如下操作:
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;
但是,即使使用良好的构造函数对false进行了很好的初始化,我也无法将其分配给@this。我也尝试像
MProduct.Constructor = (@this) => { var mole = new MProduct(@this)... };
但是这次我不能调用我的构造函数。我应该怎么办?
没有找到相关结果
已邀请:
2 个回复
奥李
。
仅模拟
方法。
查看调试输出以确认一切正常。 初始价格:10 跳过设置价格。 新价格:210 顺便说一句,您不需要在这里使用存根,
创建一个
的实例就足够了。
更新: 像这样的
类可以实现单个方法的模拟
春驹晴陪