2011-12-17 35 views
1

我已经写了一个包含System.Core和构建目标Mono/.NET 3.5的项目中的MD 2.8.5的单元测试。我真的很喜欢新NUnit的Assert.Throws,因此决定为它写一个扩展方法。我创建了一个新文件,并将其作为与测试相同的命名空间中的内容。任何人都能看到我的错误?单声道和扩展方法与MonoDevelop 2.8.5

public delegate void TestDelegate(); 

public static class AssertThrows 
{ 
    public static T Throws<T>(this Assert assert, TestDelegate td) 
     where T : Exception 
    { 
     try 
     { 
      td(); 
     } 
     catch(T e) 
     { 
      return e; 
     } 
     catch 
     { 
      throw new AssertionException("Wrong exception type."); 
     } 
     throw new AssertionException("Did not throw an error."); 
    } 
} 

MonoDevelop通过其代码完成“看到”扩展方法。但是,编译报道:

Performing main compilation... 
/Users/shamwow/dev/EngineTests.cs(19,37): error CS0117: 
    `NUnit.Framework.Assert' does not contain a definition for `Throws' 
/Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/AddIns/NUnit/nunit.framework.dll (Location of the symbol related to previous error) 


Build complete -- 1 error, 0 warnings 

(我知道MD和Mono是不一样的。)

回答

4

我假设你想使用它,就像:

Assert.Throws<FooException>(() => ...); 

扩展方法不这样工作 - 它们看起来是扩展类型上的实例方法。由于您将不会有实例Assert,因此您无法调用您的扩展方法。

+0

呃!我今天早上把它归咎于咖啡前的想法。谢谢,乔恩! – realistschuckle 2011-12-17 12:11:33