Je n'ai pas réussi à faire ce travail hors de la boîte. Je devais le peaufiner un peu. Donc, dans la méthode GrensesnittObjectLocator.GetHandler
au lieu de:
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); };
}
je l'ai modifié à:
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);
};
}
Avec cette modification en place, j'ai écrit la exmaple suivante:
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;
}
}
Vous pouvez voir comment la La classe Foo
n'a pas de constructeur par défaut. Alors maintenant, nous pouvons avoir ce test:
[InterfaceSpecification]
public class IFooTests : AppliesToAll<IFoo>
{
[Test]
public void can_add_two_numbers()
{
Assert.AreEqual(5, subject.Add(2, 3));
}
}
Et afin d'indiquer à grensesnitt
comment instancier Foo
ajouter simplement la classe suivante à votre assemblage d'essai (le même ensemble qui contient le précédent test unitaire):
[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");
});
}
}
Oui, ça fonctionne de cette façon, merci. Vous devriez faire un patch pour le git de grensesnitt (https://github.com/gregoryyoung/grensesnitt). – zzandy
@zzandy, je ne suis pas sûr que ce soit un bug afin de fournir un correctif. Peut-être que je ne sais pas comment utiliser correctement cette bibliothèque. –