如何使用鼹鼠的构造? [英] How use Moles for a constructor?

查看:181
本文介绍了如何使用鼹鼠的构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个类:

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。我想是这样的构造:

I want ModifyPrice to do nothing for a specific value, but I also want to call the constructor that 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;



不过,即使的的是具有良好的构造函数以及初始化,我不能把它分配给@this。我也试着像

But even if fake is well-initialized with the good constructor, I can't assign it to @this. I also try something like

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



但这次我不敢说我​​的构造函数。我怎么办呢?

But this time I cannot call my constructor. How am I supposed to do?

推荐答案

您并不需要模拟构造函数中,<$ C的参数的构造函数$ C>产品类已经你想要做什么。

You don't need to mock the constructor, the parameterless constructor of the Product class already does what you want.

一些调试输出添加到产品

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);
    }
}



模拟只有 ModifyPrice 方法。

[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 to confirm everything works as expected:


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



顺便说一句,你不需要在这里使用存根,

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

var fake = new SProduct { CallBase = true };



创建产品实例就足够了。

var fake = new Product();



更新:
惩戒一个方法可以与实现在 AllInstances 类像这样

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);

这篇关于如何使用鼹鼠的构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆