2014-10-09 83 views
0

我无法获取测试文件运行。我看不到我需要做不同的事情。这是C#的第一个练习项目。我想我已经解决了这个问题,但是我无法让测试文件在Microsoft Visual Studio中成功运行。目标是制定一个函数来测试年度是闰年。从练习C运行测试文件#

我想使用类年度在下面这个文件中,在我的项目是名为的Class1.cs

文件带班一年:

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

namespace Exercism 
{ 
    public class Year 
    { public bool IsLeap(int year) 
     { 
      if (year % 4 == 0) 
       if (year % 100 == 0) 
        if (year % 400 == 0) 
         return true; 
        else 
         return false; 
       else 
        return true; 
      else 
       return false; 
     } 
    } 
} 

测试文件如下。当我尝试运行它时,我在下面的每个测试函数中收到'The name'Year'在当前上下文中不存在'的消息。

using NUnit.Framework; 
using Exercism; 




[TestFixture] 
public class LeapTest 
{ 
    [Test] 
    public void Valid_leap_year() 
    { 
     Assert.That(Year.IsLeap(1996), Is.True); 
    } 
    [Ignore] 
    [Test] 
    public void Invalid_leap_year() 
    { 
     Assert.That(Year.IsLeap(1997), Is.False); 
    } 

    [Ignore] 
    [Test] 
    public void Turn_of_the_20th_century_is_not_a_leap_year() 
    { 
     Assert.That(Year.IsLeap(1900), Is.False); 
    } 

    [Ignore] 
    [Test] 
    public void Turn_of_the_25th_century_is_a_leap_year() 
    { 
     Assert.That(Year.IsLeap(2400), Is.True); 
    } 
} 

我知道这是一个基本的问题,但任何帮助,将不胜感激

回答

1

公共静态布尔IsLeap

2

您正在使用IsLeap就好像它是一个静态方法(INT年),但将其声明为实例方法。

您可以使用new Year().IsLeap(..)或将IsLeap设置为public static bool IsLeap(...)。我很确定你想要后者。

了解两者之间的差异非常重要,我建议您阅读这个主题。