2

我想测试 if-else 语句是否被执行,“if”块从字典/缓存中返回项目并返回输出,而“else”块在缓存中添加输入并返回输出

带有方法 Apply 的 IModifyBehavior 接口

我有这个课程:

namespace Decorator
{
    using System;

    /// <summary>
    /// Reverse Behavior
    /// </summary>
    public class ReverseBehavior : IModifyBehavior
    {
        /// <summary>
        /// Applies the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>result</returns>
        public string Apply(string value)
        {
            var result = string.Empty;
            if (value != null)
            {
                char[] letters = value.ToCharArray();
                Array.Reverse(letters);
                result = new string(letters); 
            }

            return result; 
        }
    }
}




using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

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

        /// <summary>
        /// The behavior
        /// </summary>
        private IModifyBehavior behavior;


        public CachingDecorator(IModifyBehavior behavior)
        {
            if (behavior == null)
            {
                throw new ArgumentNullException("behavior");
            }

            this.behavior = behavior;
        }



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

        /// <summary>
        /// Applies the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// value
        /// </returns>
        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";
                ////Note:Add(key,value)
                cache.Add(value, result); 
            }
            return result;
        }
    }
}

这是我当前的测试代码,这些代码能够通过测试,但我不确定我的实现是否正确:

[TestClass]
    public class CachingDecoratorTest
    {
        private IModifyBehavior behavior;

        [TestInitialize]
        public void Setup()
        {
            this.behavior = new CachingDecorator(new ReverseBehavior());
        }

        [TestCleanup]
        public void Teardown()
        {
            this.behavior = null;
        }

        [TestMethod]
        public void Apply_Cached_ReturnsReversedCachedValue()
        {
            string actual = "randel";           
            ////store it inside the cache
            string cached = this.behavior.Apply(actual);

            ////call the function again, to test the else block statement
            ////Implement DRY principle next time
            string expected = this.behavior.Apply(actual);
            Assert.IsTrue(cached.Equals(expected));

        }

        [TestMethod]
        public void Apply_NotCached_ReturnsReversed()
        {
            string actual = "randel";
            string expected = "lednar";
            Assert.AreEqual(expected, this.behavior.Apply(actual));
        }


    }

先生/女士,您的回答会很有帮助。谢谢++

4

3 回答 3

2

最好的方法是使用模拟框架(例如 Moq)来创建一个假IModifyBehaviour对象。

然后Apply_NotCached_ReturnsReversed测试将验证Apply模拟对象的方法是否被调用以生成结果。测试将Apply_Cached_ReturnsReversedCachedValue检查结果是否在不调用Apply模拟对象的方法的情况下返回。

实际上,您对缓存案例的测试实际上并不能证明结果来自缓存。

于 2013-03-13T04:29:54.960 回答
2

首先,我实际上会单独测试这两个类别,作为适当的单位。下面我写了我将如何测试这些。为此,我使用 NUnit 和Moq(在 Nuget 中可用)作为模拟框架。但是您可以更改测试属性并改用 MSTest。

对于反向行为,我同时涵盖了常规应用和应用于空文本:

using System;
using System.Linq;
using Decorator;
using NUnit.Framework;

namespace StackOverflow.Tests.HowToTest
{
    [TestFixture]
    public class ReverseBehaviorTest
    {
        [Test]
        public void Apply()
        {
            const string someText = "someText";
            var target = new ReverseBehavior();
            var result = target.Apply(someText);
            Assert.AreEqual(someText.Reverse(), result);
        }
        [Test]
        public void Apply_WhenNull()
        {
            var target = new ReverseBehavior();
            var result = target.Apply(null);
            Assert.AreEqual(String.Empty, result);
        }
    }
}

对于 CachingDecorator,构造函数的异常抛出,应用缓存和不应用:

using System;
using Decorator;
using Moq;
using NUnit.Framework;

namespace StackOverflow.Tests.HowToTest
{
    [TestFixture]
    public class CachingDecoratorTest
    {
        [Test]
        public void Constructor()
        {
            Assert.Throws(typeof(ArgumentNullException), () => new CachingDecorator(null));
        }

        [Test]
        public void Apply_NotCached()
        {
            var internalBehaviorMock = new Mock<IModifyBehavior>();
            internalBehaviorMock.Setup(x => x.Apply(It.IsAny<string>())).Returns<string>(y => y);
            const string someText = "someText";
            var target = new CachingDecorator(internalBehaviorMock.Object);
            target.Apply(someText);
            internalBehaviorMock.Verify(x => x.Apply(It.IsAny<string>()), Times.Once());
        }

        [Test]
        public void Apply_Cached()
        {
            var internalBehaviorMock = new Mock<IModifyBehavior>();
            internalBehaviorMock.Setup(x => x.Apply(It.IsAny<string>())).Returns<string>(y => y);
            const string someOtherText = "someOtherText";
            var target = new CachingDecorator(internalBehaviorMock.Object);
            target.Apply(someOtherText);
            target.Apply(someOtherText);
            internalBehaviorMock.Verify(x => x.Apply(It.IsAny<string>()), Times.Once());
        }
    }
}
于 2013-03-13T04:55:25.060 回答
0

只需尝试在测试用例中设置缓存字典值并在调用 Apply(string value) 方法后检查计数。

` 
       public void Apply_Cached_ReturnsReversedCachedValue()
        {
            Dictionary<string, string> cacheDict = new Dictionary<string, string>() { { "sometext", "txetemos" } };

            string str = "sometext";

            int dictionaryCountBeforeApply = cacheDict.Count();

            //set value to static cache field using reflection, here dictionary count is 1
            Type type = typeof(CachingDecorator);
            FieldInfo cacheFieldInfo = type.GetField("cache", BindingFlags.NonPublic | BindingFlags.Static);
            cacheFieldInfo.SetValue(decorator, cacheDict);

            string result = decorator.Apply(str);

            int dictionaryCountAfterApply = cacheDict.Count();

            Assert.AreEqual(dictionaryCountAfterApply, dictionaryCountBeforeApply);
        }


        public void Apply_NotCached_ReturnsReversed()
        {
            Dictionary<string, string> cacheDict = new Dictionary<string, string>() { };
            string str = "sometext";

            int dictionaryCountBeforeApply = cacheDict.Count();

            //set value to static cache field using reflection, here dictionary count is 0
            Type type = typeof(CachingDecorator);
            FieldInfo cacheFieldInfo = type.GetField("cache", BindingFlags.NonPublic | BindingFlags.Static);
            cacheFieldInfo.SetValue(decorator, cacheDict);

            string result = decorator.Apply(str);

            int dictionaryCountAfterApply = cacheDict.Count();

            Assert.AreNotEqual(dictionaryCountAfterApply, dictionaryCountBeforeApply);
        }`
于 2013-03-13T06:11:01.893 回答