2010-12-15 126 views

回答

0

我沒有設法使這個開箱即用。我不得不調整一下。所以GrensesnittObjectLocator.GetHandler方法,而不是內部:

public static Func<object> GetHandler(Type T) 
{ 
    Func<object> handler; 
    if (handleAll != null) return() => { return handleAll(T); }; 
    if (map.TryGetValue(T, out handler)) 
    { 
     return (Func<object>)handler; 
    } 
    return() => { return TryReflections(T); }; 
} 

我修改了它:

public static Func<object> GetHandler(Type T) 
{ 
    return() => 
    { 
     Func<object> handler; 
     if (handleAll != null) return handleAll(T); 
     if (map.TryGetValue(T, out handler)) 
     { 
      return handler(); 
     } 
     return TryReflections(T); 
    }; 
}  

經過這樣的修改在地方,我寫了下面的〔實施例:

public interface IFoo 
{ 
    int Add(int a, int b); 
} 

public class Foo : IFoo 
{ 
    private readonly string _foo; 
    public Foo(string foo) 
    { 
     _foo = foo; 
    } 

    public int Add(int a, int b) 
    { 
     return a + b; 
    } 
} 

你可以看到如何Foo類沒有默認的構造函數。所以,現在我們可以有這樣的測試:

[InterfaceSpecification] 
public class IFooTests : AppliesToAll<IFoo> 
{ 
    [Test] 
    public void can_add_two_numbers() 
    { 
     Assert.AreEqual(5, subject.Add(2, 3)); 
    } 
} 

而且爲了指示grensesnitt如何實例Foo只是下面的類添加到您的測試組件(包含以前的單元測試相同的程序集):

[SetUpFixture] 
public class Config 
{ 
    [SetUp] 
    public void SetUp() 
    { 
     // indicate to Grensesnitt that the Foo class 
     // doesn't have a default constructor and that 
     // it is up to you to provide an instance 
     GrensesnittObjectLocator.Register<Foo>(() => 
     { 
      return new Foo("abc"); 
     }); 
    } 
} 
+0

是的,它的工作方式,謝謝。你應該爲grensesnitt的git做一個補丁(https://github.com/gregoryyoung/grensesnitt)。 – zzandy 2010-12-22 13:13:19

+0

@zzandy,我不確定這是否是一個錯誤,以便提供補丁。也許我只是不知道如何正確使用這個庫。 – 2010-12-22 13:14:49

相關問題