相关疑难解决方法(0)

C#如何正确单元测试一个遵循装饰器模式的类?

我对单元测试比较陌生(我实际上正在研究它)

我的目标当然是能够在下面的课程中测试方法.

该类只是检查输入是否已经在缓存中,如果输入不在缓存中,它将返回输入的反转形式(虽然实现不在这里,但假设确实如此,因为目的只是为了测试).

基本上,目标是确保测试if-else.

这是我的班级:

namespace YouSource.Decorator
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// Caching Decorator
    /// </summary>
    public class CachingDecorator : IModifyBehavior
    {
       private IModifyBehavior behavior;

       private static Dictionary<string, string> cache = 
           new Dictionary<string, string>();

        public string Apply(string value)
        {
            ////Key = original value, Value = Reversed
            var result = string.Empty;

            //cache.Add("randel", "lednar");
            if(cache.ContainsKey(value))
            {

                result = cache[value];

            }
            else
            {

                result = this.behavior.Apply(value);// = "reversed";
                cache.Add(value, result); 
            }
            return result;
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing design-patterns

5
推荐指数
1
解决办法
3235
查看次数

标签 统计

.net ×1

c# ×1

design-patterns ×1

unit-testing ×1