2010-12-15 128 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

相关问题