2014-10-09 44 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(...)。我很確定你想要後者。

瞭解兩者之間的差異非常重要,我建議您閱讀這個主題。