无法使用NUnit框架测试静态方法。

huangapple go评论59阅读模式
英文:

Unable to test a static method using NUnit framework

问题

Static class Clock 代码部分不需要翻译,以下是关于错误信息的翻译:

错误信息:
Severity.Code.Description.Project.File.Line.Suppression State
错误.CS0234.在命名空间 'Clock' 中不存在类型或命名空间名 'Tick'(是否丢失了一个程序集引用?).NUnitTest

这个错误表明在 NUnit 测试中,尝试使用 Clock.Tick() 时出现问题,提示无法找到 'Tick' 方法或命名空间 'Clock',可能是因为缺少程序集引用。

英文:

I am trying to use NUnit to test the methods (in this case Tick() ) in a static class Clock but it simply returns an error stating that that method does not exist in the namespace. I have made sure to use the same namespace for my source project and my NUnit test as well as adding a reference of the NUnit test to the source code and using NUnit.Framework;

Static class Clock:


namespace Clock
{
    static class Clock
    {
        private static Counter[] _counters;

        static Clock()
        {
            _counters = new Counter[3];
            _counters[0] = new Counter("Second counter");
            _counters[1] = new Counter("Minute counter");
            _counters[2] = new Counter("Hour counter");
        }

        public static void Tick()
        {
            _counters[0].Increment(); 

            if (_counters[0].Ticks == 60)
            {
                _counters[0].Reset();
                _counters[1].Increment();
            }

            if (_counters[1].Ticks == 60)
            {
                _counters[1].Reset();
                _counters[2].Increment(); 
            }

            if (_counters[2].Ticks == 24)
            {
                _counters[2].Reset();
            }
        }

    }
}

When I try to use Clock.Tick() in NUnit test, the error occured:

      [Test]
        public void TestTick()
        {
            Clock.Tick();
            Assert.AreEqual(1, Clock.GetCounter(0).Ticks); 
            Assert.AreEqual(0, Clock.GetCounter(1).Ticks); 
            Assert.AreEqual(0, Clock.GetCounter(2).Ticks); 
        }

Specific Error:
Severity Code Description Project File Line Suppression State
Error CS0234 The type or namespace name 'Tick' does not exist in the namespace 'Clock' (are you missing an assembly reference?) NUnitTest

答案1

得分: 3

将 "puts public in front of static, it cannot be access by its protection level" 翻译为:将 "public" 放在 "static" 前面,它将无法通过其保护级别访问。

英文:

puts public in front of static, it cannot be access by its protection level

huangapple
  • 本文由 发表于 2023年3月1日 16:45:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定